这不是在 php 中构建 rest api 的原始方法,这将帮助您启动它。
这里有main你只需要在php中创建rest类,我已经在php中给出了主要的rest类。
<?php
class REST {
// initialize the database connection
const DB_SERVER = "localhost";
const DB_USER = "root";
const DB_PASSWORD = "pass";
const DB = "vtms";
// set the conntent type and object to the api
public $_allow = array();
public $_content_type = "application/json";
public $_request = array();
private $_method = "";
private $_code = 200;
//construct the input for the rest call
public function __construct(){
$this->inputs();
}
//Set reference to the webserver
public function get_referer(){
return $_SERVER['HTTP_REFERER'];
}
// give the status of the rest operation
public function response($data,$status){
$this->_code = ($status)?$status:200;
$this->set_headers();
echo $data;
exit;
}
// For a list of http codes checkout http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
private function get_status_message(){
$status = array(
200 => 'OK',
201 => 'Created',
204 => 'No Content',
404 => 'Not Found',
406 => 'Not Acceptable');
return ($status[$this->_code])?$status[$this->_code]:$status[500];
}
public function get_request_method(){
return $_SERVER['REQUEST_METHOD'];
}
// define curd operation of the api
private function inputs(){
switch($this->get_request_method()){
case "POST":
$this->_request = $this->cleanInputs($_POST);
break;
case "GET":
case "DELETE":
$this->_request = $this->cleanInputs($_GET);
break;
case "PUT":
parse_str(file_get_contents("php://input"),$this->_request);
$this->_request = $this->cleanInputs($this->_request);
break;
default:
$this->response('',406);
break;
}
}
private function cleanInputs($data){
$clean_input = array();
if(is_array($data)){
foreach($data as $k => $v){
$clean_input[$k] = $this->cleanInputs($v);
}
}else{
if(get_magic_quotes_gpc()){
$data = trim(stripslashes($data));
}
$data = strip_tags($data);
$clean_input = trim($data);
}
return $clean_input;
}
// set headers to the content
private function set_headers(){
header("HTTP/1.1 ".$this->_code." ".$this->get_status_message());
header("Content-Type:".$this->_content_type);
}
}
?>
如果您复制并粘贴此代码而几乎没有任何错误,那么现在创建一个对象类扩展至此 api,例如。
<?php
require_once("Rest.inc.php");
类 API 扩展 REST {
public $data = "";
private $db = NULL;
private $mysqli = NULL;
public function __construct() {
parent::__construct(); // Init parent contructor
$this->dbConnect(); // Initiate Database connection
}
/*
* Connect to Database
*/
private function dbConnect() {
$this->mysqli = new mysqli(self::DB_SERVER, self::DB_USER, self::DB_PASSWORD, self::DB);
}
/*
* Dynmically call the method based on the query string
*/
public function processApi() {
$func = strtolower(trim(str_replace("/", "", $_REQUEST['x'])));
if ((int) method_exists($this, $func) > 0) {
$this->$func();
} else {
$this->response('', 404);
} // If the method not exist with in this class "Page not found".
}
private function getAllstudent() {
if ($this->get_request_method() != "GET") {
$this->response('', 406);
}
$query = "SELECT * from students";
$r = $this->mysqli->query($query) or die($this->mysqli->error . __LINE__);
if ($r->num_rows > 0) {
$result = array();
while ($row = $r->fetch_assoc()) {
$result[] = $row;
}
$this->response($this->json($result), 200); // send user details
}
$this->response('', 204); // If no records "No Content" status
}
private function json($data){
if(is_array($data)){
return json_encode($data);
}
}
}
$api = new API;
$api->processApi();
只需打开一个 .htaccess 文件并输入此代码
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^([a-zA-Z0-9-]+)/studentapi/method/ student.php?x=$1 [QSA,NC,L]
</IfModule>
创建表调用 student 并向其输入一些值,现在打开浏览器并输入此 URL
http://localhost/yourserviceFodername/getAllStudent/studentapi/method/student。
开心点...