这不是一个原始的方式来建立在PHP的休息API,这将帮助你启动它。
这里有主要的你需要在php中创建休息类, 我已经给出了PHP中的主休息类。
<?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);
}
}
?>
如果你复制粘贴此代码没有你几乎做任何错误,现在创建一个类的对象扩展到这个REST 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>
创建表调用学生和输入一些值给它,而现在你打开浏览器,键入这个URL, http://localhost/yourserviceFodername/getAllStudent/studentapi/method/student。
振作起来......
您可以使用卷曲 - http://php.net/manual/en/book.curl.php – Gntem
你居然主办的PHP文件?你确实有一个Web服务器或其他东西在运行?能够从磁盘执行一个PHP文件是分开执行它使用url –