我创建了一个API项目,该项目在我的xampp本地主机中运行正常,但是如何通过Web托管将其部署到Internet,我已经准备好托管和域了,但是我已经在线搜索了,但是所有类似的项目我看到使用本地主机时,他们都没有谈论如何将其部署到在线服务。这是PHP代码,以防万一我需要更改它们。这是配置文件
$db_user = 'root';
$db_passwoord = '';
$db_name = 'phprest';
$db = new PDO('mysql:host=127.0.0.1;dbname='.$db_name.';charset=utf8',$db_user,$db_passwoord);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
define('APP_NAME', 'PHP REST API TUTORIAL');
初始化文件
defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);
defined('SITE_ROOT') ? null : define('SITE_ROOT', DS .'xampp' .DS. 'htdocs'.DS.'phprest');
defined('INC_PATH') ? null : define('INC_PATH', SITE_ROOT.DS. 'includes');
defined('CORE_PATH') ? null : define('CORE_PATH', SITE_ROOT.DS. 'core');
require_once(INC_PATH.DS."config.php");
require_once(CORE_PATH.DS."post.php");
我的帖子文件
private $conn;
private $table = 'posts';
public $id;
public $category_id;
public $category_name;
public $title;
public $body;
public $author;
public $created_at;
public function __construct($db){
$this->conn = $db;
}
public function read(){
$query = 'SELECT
c.name as category_name,
p.id,
p.category_id,
p.title,
p.body,
p.author,
p.created_at
FROM
' .$this->table . ' p
LEFT JOIN
categories c ON p.category_id = c.id
ORDER BY p.created_at DESC';
$stmt = $this->conn->prepare($query);
$stmt->execute();
return $stmt;
}
我的读取数据文件
<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
include_once('../core/initialize.php');
$post = new Post($db);
$result = $post->read();
$num = $result->rowCount();
if($num > 0){
$post_arr = array();
$post_arr['data'] = array();
while($row = $result->fetch(PDO::FETCH_ASSOC)){
extract($row);
$post_item = array(
'id' => $id,
'title' =>$title,
'body' => html_entity_decode($body),
'author' => $author,
'category_id' => $category_id,
'category_name' => $category_name,
);
array_push($post_arr['data'], $post_item);
}
echo json_encode($post_arr);
} else{
echo json_encode(array('message' => 'No posts found.'));
}
这里是到数据库文件https://drive.google.com/open?id=1HgIYk0zIm9ZltMdAxJlDaGceWk-Po9J9的链接
您的网络主机应该已经为您提供了FTP登录服务器的登录凭据以及MySQL的凭据。