php 建立簡單的Restful WebAPI(三)

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   strong   ar   for   

  上篇記錄了怎樣實現route,本篇記錄怎麼實現request,response。

  Request 處理請求

<?phpclass Request{    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 function processRequest()    {        $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 function setID($id)      {          $this->ID = $id;      }        public function setData($data)      {          $this->data = $data;      }        public function setMethod($method)      {          $this->method = $method;      }        public function setRequestVars($request_vars)      {          $this->request_vars = $request_vars;      }    public function getID()      {          return $this->ID;      }        public function getData()      {          return $this->data;      }        public function getMethod()      {          return $this->method;      }        public function getHttpAccept()      {          return $this->http_accept;      }        public function getRequestVars()      {          $this->request_vars[‘id‘] = $this->ID;        return $this->request_vars;      }  }  ?>

  代碼十分簡單,就是從request擷取請求method,querystring,請求body資料(json格式)。

  Response 發送響應

<?phpclass Response{    public function __construct()      {    }    public function sendResponse($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 function getStatusCodeMessage($status)      {          $codes = Array(              100 => ‘Continue‘,              101 => ‘Switching Protocols‘,              200 => ‘OK‘,              201 => ‘Created‘,              202 => ‘Accepted‘,              203 => ‘Non-Authoritative Information‘,              204 => ‘No Content‘,              205 => ‘Reset Content‘,              206 => ‘Partial Content‘,              300 => ‘Multiple Choices‘,              301 => ‘Moved Permanently‘,              302 => ‘Found‘,              303 => ‘See Other‘,              304 => ‘Not Modified‘,              305 => ‘Use Proxy‘,              306 => ‘(Unused)‘,              307 => ‘Temporary Redirect‘,              400 => ‘Bad Request‘,              401 => ‘Unauthorized‘,              402 => ‘Payment Required‘,              403 => ‘Forbidden‘,              404 => ‘Not Found‘,              405 => ‘Method Not Allowed‘,              406 => ‘Not Acceptable‘,              407 => ‘Proxy Authentication Required‘,              408 => ‘Request Timeout‘,              409 => ‘Conflict‘,              410 => ‘Gone‘,              411 => ‘Length Required‘,              412 => ‘Precondition Failed‘,              413 => ‘Request Entity Too Large‘,              414 => ‘Request-URI Too Long‘,              415 => ‘Unsupported Media Type‘,              416 => ‘Requested Range Not Satisfiable‘,              417 => ‘Expectation Failed‘,              500 => ‘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] : ‘‘;      }}  ?>

  產生response。

  這兩個類為工具類,實現也不複雜,功能單一,需要繼續豐富修改。

php 建立簡單的Restful WebAPI(三)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.