久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      PHP如何實(shí)現(xiàn)RESTful原理

      PHP如何實(shí)現(xiàn)RESTful原理

      PHP如何實(shí)現(xiàn)RESTful原理

      首先定義一個(gè)請(qǐng)求數(shù)據(jù)接收類;然后請(qǐng)求類中根據(jù)請(qǐng)求類型,進(jìn)行訪問不通方法;

       class Request   {       // 允許的請(qǐng)求方式       private static $method_type = array('get', 'post', 'put', 'patch', 'delete');        // 測(cè)試數(shù)據(jù)       private static $test_class = array(           1 => array('name'=>'測(cè)試一班','count'=>18),            2 => array('name'=>'測(cè)試二班','count'=>15)       );        public static function getRequest()       {           // 請(qǐng)求方法           $method = strtolower($_SERVER['REQUEST_METHOD']);           if (in_array($method, self::$method_type)) {               // 調(diào)用請(qǐng)求方法對(duì)應(yīng)的方法               $data_name = $method . "Data";               return self::$data_name($_REQUEST);           }           return false;       }        // GET 獲取信息       private static function getData($request_data)       {           $class_id = (int)$request_data['class'];           if ($class_id > 0) {               // GET /class/ID: 獲取某個(gè)指定班的信息               return self::$test_class[$class_id];           }else{                // GET /class: 列出所有班級(jí)               return self::$test_class;           }       }        // POST /class 新建一個(gè)班級(jí)       private static function postData($request_data)       {           $class_id = (int)$request_data['class'];           if ($class_id == 0) {               return false;           }           $data = array();           if (!empty($request_data['name']) && isset($request_data['count'])) {               $data['name'] = $request_data['name'];               $data['count'] = $request_data['count'];               self::$test_class[] = $data;               return self::$test_class;            }else{               return false;           }       }        // PUT /class/ID 更新某個(gè)指定班級(jí)的信息(全部信息)       private static function putData($request_data)       {           $class_id = (int)$request_data['class'];           if ($class_id == 0) {               return false;           }            $data = array();           if (!empty($request_data['name']) && isset($request_data['count'])) {               $data['name'] = $request_data['name'];               $data['count'] = (int)$request_data['count'];               self::$test_class[$class_id] = $data;               return self::$test_class;           }else{               return false;           }       }        // PATCH /class/ID 更新某個(gè)指定班級(jí)的信息 (部分信息)       private static function pacthData($request_data)       {           $class_id = (int)$request_data['class'];           if ($class_id == 0) {               return false;           }           if (!empty($request_data['name'])) {               self::$test_class[$class_id]['name'] = $request_data['name'];           }           if (isset($request_data['count'])) {               self::$test_class[$class_id]['count'] = $request_data['count'];           }           return self::$test_class;       }        // DELETE /class/ID 刪除某個(gè)班       private static function deleteData($request_data)       {           $class_id = (int)$request_data['class'];           if ($class_id == 0) {               return false;           }           unset(self::$test_class[$class_id]);           return self::$test_class;       }   }

      再定義一個(gè)數(shù)據(jù)輸出類,將數(shù)據(jù)輸出的格式進(jìn)行統(tǒng)一的封裝;最后將方法返回的數(shù)據(jù)進(jìn)行輸出即可。

        <?php   /**   * 包含一個(gè)Response類,即輸出類。根據(jù)接收到的Content-Type,將Request類返回的數(shù)組拼接成對(duì)應(yīng)的格式,加上header后輸出   */   class Response   {       const HTTP_VERSION = "HTTP/1.1";          public function sendResponse($data)       {           // 獲取數(shù)據(jù)           if ($data) {               $code = 200;               $message = "OK";           }else{               $code = 404;               $data = array('error' => "Not Found");               $message = "Not Found";           }              header(self::HTTP_VERSION . " $code $message");           $content_type = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : $_SERVER['HTTP_ACCEPT'];           if (strpos($content_type, 'application/json') !== false) {               header("Content-Type: application/json");               echo self::encodeJson($data);           }elseif (strpos($content_type, 'application/xml') !== false) {               header("Content-Type: application/xml");               echo self::encodeXml($data);           }else{               header("Content-Type: text/html");               echo self::encodeHtml($data);           }       }          // json 格式       private static function encodeJson($responseData)       {           return json_encode($responseData);       }          // xml 格式       private static function encodeXml($responseData)       {           $xml = new SimpleXMLElement('<?xml version="1.0"?><rest></rest>');           foreach ($responseData as $key => $value) {               if (is_array($value)) {                   foreach ($value as $k => $v) {                       $xml->addChild($k,$v);                   }               }else{                   $xml->addChild($key,$value);               }           }           return $xml->asXML();       }          // html 格式       private static function encodeHtml($responseData)       {           $html = "<table border='1'>";           foreach ($responseData as $key => $value) {               $html .= "<tr>";               if (is_array($value)) {                   foreach ($value as $k => $v) {                       $html .= "<td>$k</td><td>$v</td>";                   }               }else{                   $html .= "<td>$key</td><td>$value</td>";               }               $html .= "</tr>";           }           $html .="</table>";           return $html;       }   }   ?>

      推薦教程:《PHP教程》

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)