国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 建站經驗 > 正文

php基于curl擴展制作跨平臺!的restfule 接口

2019-11-02 15:38:27
字體:
來源:轉載
供稿:網友

   這篇文章主要介紹了php基于curl擴展制作跨平臺的restfule 接口的相關資料以及詳細的代碼,有需要的小伙伴可以參考下。

  restfule 接口

  適用的平臺:跨平臺

  所依賴:curl擴展

  git:https://git.oschina.net/anziguoer/restAPI

  ApiServer.php

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 <?php /** * @Author: yangyulong * @Email : [email protected] * @Date: 2015-04-30 05:38:34 * @Last Modified by: yangyulong * @Last Modified time: 2015-04-30 17:14:11 */   class apiServer { /** * 客戶端請求的方式 * @var string */ private $method = '';   /** * 客戶端發送的數據 * @var [type] */ protected $param;   /** * 要操作的資源 * @var [type] */ protected $resourse;   /** * 要操作的資源id * @var [type] */ protected $resourseId;     /** * 構造函數, 獲取client 請求的方式,以及傳輸的數據 * @param object 可以自定義傳入的對象 */ public function __construct() { //首先對客戶端的請求進行驗證 $this->authorization();   $this->method = strtolower($_SERVER['REQUEST_METHOD']);   //所有的請求都是pathinfo模式 $pathinfo = $_SERVER['PATH_INFO'];   //將pathinfo數據信息映射為實際請求方法 $this->getResourse($pathinfo);   //獲取傳輸的具體參數 $this->getData();   //執行響應 $this->doResponse(); }   /** * 根據不同的請求方式,獲取數據 * @return [type] */ private function doResponse(){ switch ($this->method) { case 'get': $this->_get(); break; case 'post': $this->_post(); break; case 'delete': $this->_delete(); break; case 'put': $this->_put(); break; default: $this->_get(); break; } }   // 將pathinfo數據信息映射為實際請求方法 private function getResourse($pathinfo){   /** * 將pathinfo數據信息映射為實際請求方法 * GET /users: 逐頁列出所有用戶; * POST /users: 創建一個新用戶; * GET /users/123: 返回用戶為123的詳細信息; * PUT /users/123: 更新用戶123; * DELETE /users/123: 刪除用戶123; * * 根據以上規則,將pathinfo第一個參數映射為需要操作的數據表, * 第二個參數映射為操作的id */   $info = explode('/', ltrim($pathinfo, '/')); list($this->resourse, $this->resourseId) = $info; }   /** * 驗證請求 */ private function authorization(){ $token = $_SERVER['HTTP_CLIENT_TOKEN']; $authorization = md5(substr(md5($token), 8, 24).$token); if($authorization != $_SERVER['HTTP_CLIENT_CODE']){ //驗證失敗,輸出錯誤信息給客戶端 $this->outPut($status = 1); } }   /** * [getData 獲取傳送的參數信息] * @param [type] $pad [description] * @return [type] [description] */ private function getData(){ //所有的參數都是get傳參 $this->param = $_GET; }   /** * 獲取資源操作 * @return [type] [description] */ protected function _get(){ //邏輯代碼根據自己實際項目需要實現 }   /** * 新增資源操作 * @return [type] [description] */ protected function _post(){ //邏輯代碼根據自己實際項目需要實現 }   /** * 刪除資源操作 * @return [type] [description] */ protected function _delete(){ //邏輯代碼根據自己實際項目需要實現 }   /** * 更新資源操作 * @return [type] [description] */ protected function _put(){ //邏輯代碼根據自己實際項目需要實現 }   /** * 出入服務端返回的數據信息 json格式 */ public function outPut($stat, $data=array()){ $status = array( //0 狀態表示請求成功 0 => array( 'code' => 1, 'info' => '請求成功', 'data' =>$data ), //驗證失敗 1 => array( 'code' => 0, 'info' => '請求不合法' ) );   try{ if(!in_array($stat, array_keys($status))){ throw new Exception('輸入的狀態碼不合法'); }else{ echo json_encode($status[$stat]); } }catch (Exception $e){ die($e->getMessage()); } } }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 夏河县| 隆安县| 拜泉县| 崇州市| 衡东县| 乐山市| 永川市| 六安市| 额济纳旗| 东港市| 赞皇县| 阜城县| 泊头市| 漠河县| 莲花县| 牙克石市| 邢台县| 新沂市| 丹阳市| 黑河市| 天镇县| 大埔县| 康平县| 博白县| 巴楚县| 棋牌| 集贤县| 海口市| 葵青区| 泾川县| 三都| 永胜县| 贺州市| 荆州市| 普宁市| 五指山市| 峨边| 峨边| 三原县| 堆龙德庆县| 铜陵市|