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

首頁 > 編程 > PHP > 正文

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

2020-03-22 19:03:01
字體:
來源:轉載
供稿:網友
* html' target='_blank'>構造函數, 獲取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());}ApiClient.php * Created by PhpStorm. * User: anziguoer@sina.com * Date: 2015/4/29 * Time: 12:36 * link: http://www.ruanyifeng.com/blog/2014/05/restful_api.html [restful設計指南]/*** * * * * * * * * * * * * * * * * * * * * * * * * * ***/ * 定義路由的請求方式 * * $url_model=0 * * 采用傳統的URL參數模式 * * http://serverName/appName/ m=module&a=action&id=1 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * PATHINFO模式(默認模式) * * 設置url_model 為1 * * http://serverName/appName/module/action/id/1/ * ** * * * * * * * * * * * * * * * * * * * * * * * * * * **class restClient //請求的token const token='yangyulong'; //請求url private $url; //請求的類型 private $requestType; //請求的數據 private $data; //curl實例 private $curl; public $status; private $headers = array(); * [__construct 構造方法, 初始化數據] * @param [type] $url 請求的服務器地址 * @param [type] $requestType 發送請求的方法 * @param [type] $data 發送的數據 * @param integer $url_model 路由請求方式 public function __construct($url, $data = array(), $requestType = 'get') { //url是必須要傳的,并且是符合PATHINFO模式的路徑 if (!$url) { return false; $this- requestType = strtolower($requestType); $paramUrl = ''; // PATHINFO模式 if (!empty($data)) { foreach ($data as $key = $value) { $paramUrl.= $key . '=' . $value.'&'; $url = $url .' '. $paramUrl; //初始化類中的數據 $this- url = $url; $this- data = $data; try{ if(!$this- curl = curl_init()){ throw new Exception('curl初始化錯誤:'); }catch (Exception $e){ echo ' pre print_r($e- getMessage()); echo ' /pre curl_setopt($this- curl, CURLOPT_URL, $this- url); curl_setopt($this- curl, CURLOPT_RETURNTRANSFER, 1); * [_post 設置get請求的參數] * @return [type] [description] public function _get() { * [_post 設置post請求的參數] * post 新增資源 * @return [type] [description] public function _post() { curl_setopt($this- curl, CURLOPT_POST, 1); curl_setopt($this- curl, CURLOPT_POSTFIELDS, $this- data); * [_put 設置put請求] * put 更新資源 * @return [type] [description] public function _put() { curl_setopt($this- curl, CURLOPT_CUSTOMREQUEST, 'PUT'); * [_delete 刪除資源] * delete 刪除資源 * @return [type] [description] public function _delete() { curl_setopt($this- curl, CURLOPT_CUSTOMREQUEST, 'DELETE'); * [doRequest 執行發送請求] * @return [type] [description] public function doRequest() { //發送給服務端驗證信息 if((null !== self::token) && self::token){ $this- headers = array( 'Client_Token: '.self::token, 'Client_Code: '.$this- setAuthorization() //發送頭部信息 $this- setHeader(); //發送請求方式 switch ($this- requestType) { case 'post': $this- _post(); break; case 'put': $this- _put(); break; case 'delete': $this- _delete(); break; default: curl_setopt($this- curl, CURLOPT_HTTPGET, TRUE); break; //執行curl請求 $info = curl_exec($this- curl); //獲取curl執行狀態信息 $this- status = $this- getInfo(); return $info; * 設置發送的頭部信息 private function setHeader(){ curl_setopt($this- curl, CURLOPT_HTTPHEADER, $this- headers); * 生成授權碼 * @return string 授權碼 private function setAuthorization(){ $authorization = md5(substr(md5(self::token), 8, 24).self::token); return $authorization; * 獲取curl中的狀態信息 public function getInfo(){ return curl_getinfo($this- curl); * 關閉curl連接 public function __destruct(){ curl_close($this- curl);}

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 天祝| 和顺县| 桃江县| 安多县| 营山县| 合江县| 水富县| 通城县| 安康市| 汪清县| 青田县| 抚松县| 雷州市| 登封市| 莒南县| 潍坊市| 佳木斯市| 许昌县| 永城市| 南皮县| 定边县| 岱山县| 琼海市| 木里| 贞丰县| 瓮安县| 漳州市| 阜新市| 昭觉县| 新余市| 五原县| 白城市| 措勤县| 玉溪市| 石林| 肃宁县| 延庆县| 凤城市| 临漳县| 黄浦区| 蚌埠市|