The original is here: http://hmw.iteye.com/blog/1190827
TP comes with Api,get request interface
/** * Show Resource list * * @return \think\response */Public Function index (Request $request) {//$requ Est_method = Strtolower ($_server[' request_method ');//$return _onj = new Restrequest ();//$data = db (' Shopgo ODS ')->select (); $data = restutils::p rocessrequest (); $method = $data->getmethod ();//Print_r ($method); Switch ($method) {case ' get ': $user _list = db (' Shopgoods ')->limit (2)->select ();// Print_r ($user _list); if ($data->gethttpaccept () = = ' json ') {restutils::sendresponse ($, Json_encode ($user _list), ' Applica Tion/json '); }else if ($data->gethttpaccept () = = ' xml ') {//using the Xml_serializer Pear package $options = Array (' indent ' = ', ' adddecl ' = false,//' Rootname ' and $FC->getaction (), Xml_serializer_option_return_result = true); $serializer = new Xml_serializer ($options); Restutils::sendresponse ($serializer->serialize ($user _list), ' application/xml '); } break; Case ' post ': $user = new User (); $user->setfirstname ($data->getdata ()->first_name); Just for example, this should is done cleaner//And so on ... $user->save (); Break etc, etc, etc ... } }
2, the method of implementing interface request processing and request Processing "
<?php/** * Created by Phpstorm. * User:administrator * DATE:2017/6/26 * time:10:18 */namespace app\api\controller;use app\api\controller\RestRequest;/ /Simple class to store some information about the request (Restrequest), and a class that provides several static methods to handle requests and responses class restutils{//To process JSON or XML based on the requested content type, But let's be a little simpler now. Then, the method we process the request will be similar to the public static function ProcessRequest () {//Get US verb get action $request _method = Strto Lower ($_server[' request_method '); $return _obj = new Restrequest (); We'll store the request data here $data = Array (); Switch ($request _method) {//gets is easy ... case ' get ': $data = $_get; Break So is posts case ' post ': $data = $_post; Break Here's the tricky bit ... case ' put '://Basically, we read a string from PHP ' s special input Location,//And then parse it out to an array viaParse_str ... Per the PHP docs://Parses STR as if it were the query string passed via a URL and sets// Variables in the current scope. Parse_str (file_get_contents (' Php://input '), $put _vars); $data = $put _vars; Break Case ' delete ': break; }//Store the method $return _obj->setmethod ($request _method); Set the raw data, so we can access it if needed (there is//other pieces to your requests) $return _o Bj->setrequestvars ($data); if (Isset ($data [' data ')]) {//Translate the JSON to a Object for use however you want $return _OBJ-&G T;setdata (Json_decode ($data [' data ')]); } return $return _obj; public static function Sendresponse ($status = $, $body = ", $content _type = ' text/html ') {$status _header = ' http/1.1 '. $status. ‘ ‘ . Restutils::getstatuscodemessage ($status); //Set the status header ($status _header); Set the content Type header (' Content-type: '. $content _type); Pages with body is easy if ($body! = ") {//Send the body echo $body; Exit }//We need to create the body if none is passed else {//create some body messages $message = "; This is purely optional, but makes the pages a little nicer to read//for your users. Since won ' t likely send a lot of the different status codes,//This also shouldn ' t is too ponderous to Maintai n Switch ($status) {case 401: $message = ' must is authorized to view this Page. '; Break Case £ º $message = ' You must is authorized to the view this page. 200 '; Break Case 404: $message = ' The requested URL‘ . $_server[' Request_uri '. ' is not found. '; Break Case A: $message = ' The server encountered an error processing your request. '; Break Case 501: $message = ' The requested method was not implemented. '; Break }//Servers don ' t always has a signature turned on (this was an Apache directive "serversignature on") $signature = ($_server[' server_signature '] = = ")? $_server[' Server_software '. ' Server at '. $_server[' server_name '. ' Port '. $_server[' server_port ': $_server[' server_signature ']; This should is templatized in a real-world solution $body = ' <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//en" "HTTP://WWW.W3.ORG/TR/HTML4/STRICT.DTD" >
<?php/** * Created by Phpstorm. * User:administrator * DATE:2017/6/26 * time:10:18 */namespace app\api\controller;use app\api\controller\restutils;//Jane The single class to store some information about the request (Restrequest), and a class that provides several static methods to handle requests and responses to class restrequest{private $request _vars; Private $data; Private $http _accept; Private $method; Public Function __construct () {$this->request_vars = array (); $this->data = "; $this->http_accept = ' json ';//(Strpos ($_server[' http_accept '), ' json ')? ' JSON ': ' XML '; $this->method = ' get '; The Public Function SetData ($data) {$this->data = $data; The Public Function Setmethod ($method) {$this->method = $method; The Public Function setrequestvars ($request _vars) {$this->request_vars = $request _vars; } public Function GetData () {return $this->data; } public Function GetMethod () {return $this->method; } public Function gethttpaccept () {RETurn $this->http_accept; } public Function Getrequestvars () {return $this->request_vars; }}
Create a rest API using TP5