PHP creates a simple restful WebAPI (iii)

Source: Internet
Author: User

The previous article recorded how to implement the route, this document how to achieve request,response.

  Request Processing Requests

<?PHPclassrequest{Private $request _vars; Private $data; Private $http _accept; Private $method; Private $ID;  Public function__construct ($id=NULL)      {          $this->request_vars =Array(); $this->data =Array(); $this->http_accept = ' Application/json '; $this->method = ' Get '; $this->id =$id; $this-ProcessRequest (); }       Public functionProcessRequest () {$request _method=Strtolower($_server[' Request_method ']); $this->setmethod ($request _method); $request _vars=$_get; if($request _vars! = "){            $this->setrequestvars ($request _vars); }          $data=file_get_contents(' Php://input '); if($data! = "){            $this->setdata (Json_decode ($data,TRUE)); }     }      Public functionSetID ($id)      {          $this->id =$id; }         Public functionSetData ($data)      {          $this->data =$data; }         Public functionSetmethod ($method)      {          $this->method =$method; }         Public functionSetrequestvars ($request _vars)      {          $this->request_vars =$request _vars; }     Public functionGetID () {return $this-ID; }         Public functionGetData () {return $this-data; }         Public functionGetMethod () {return $this-method; }         Public functiongethttpaccept () {return $this-http_accept; }         Public functionGetrequestvars () {$this->request_vars[' id '] =$this-ID; return $this-Request_vars; }  }  ?>

The code is simple, which is to get the request method,querystring from request, requesting the body data (in JSON format).

  Response Send Response

<?PHPclassresponse{ Public function__construct () {} Public functionSendresponse ($status= 200,$body= ",$content _type= ' Application/json ')     {         $status _header= ' http/1.1 '.$status. ‘ ‘ .$this->getstatuscodemessage ($status); Header($status _header); Header(' Content-type: '.$content _type. ‘; Charset=utf-8 '); Echo $body; Exit; }     Public functionGetstatuscodemessage ($status)      {          $codes=Array ( ' Continue ', 101 = ' Switching Protocols ', ' OK ', 201 = ' C  Reated ', 202 = ' Accepted ', 203 = ' non-authoritative information ', 204 = ' No content ', 205 = ' Reset content ', 206 = ' Partial content ', + = ' M Ultiple Choices ', 301 = ' Moved permanently ', 302 = ' Found ', 303 = ' See              Other ', 304 = ' not Modified ', 305 = ' Use Proxy ', 306 = ' (Unused) ',              307 = ' Temporary Redirect ', and ' bad Request ', 401 = ' Unauthorized ',              402 = ' Payment Required ', 403 = ' Forbidden ', 404 = ' not Found ', 405 = ' Method not allowed ', 406 = ' not acceptable ', 407 = ' Proxy authentication Re Quired ', 408 = ' Request Timeout ', 409 = ' Conflict ', 410 = ' Gone ', 411 = ' Lengt              H Required ', 412 = ' precondition Failed ', 413 = ' Request Entity Too Large ', 414 = ' Request-uri Too Long ', 415 = ' Unsupported Media Type ', 416 = ' Requested Rang              E not satisfiable ', 417 = ' expectation Failed ', and ' Internal Server Error ',              501 = ' Not implemented ', 502 = ' bad Gateway ', 503 = ' Service unavailable ', 504 = ' Gateway Timeout ', 505 = ' HTTP Version not supported '          ); return(isset($codes[$status])) ?$codes[$status] : ‘‘; }}  ?>

Generate response.

These two classes are tool classes, implementation is not complex, functional single, need to continue to enrich the changes.

PHP creates a simple restful WebAPI (iii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.