php基于curl擴(kuò)展制作跨平臺(tái)的restfule 接口
restfule 接口
適用的平臺(tái):跨平臺(tái)
所依賴(lài):curl擴(kuò)展
git:https://git.oschina.net/anziguoer/restAPI
ApiServer.php
<?php /** * @Author: yangyulong * @Email : anziguoer@sina.com * @Date: 2015-04-30 05:38:34 * @Last Modified by: yangyulong * @Last Modified time: 2015-04-30 17:14:11 */ class apiServer { /** * 客戶(hù)端請(qǐng)求的方式 * @var string */ private $method = ''; /** * 客戶(hù)端發(fā)送的數(shù)據(jù) * @var [type] */ protected $param; /** * 要操作的資源 * @var [type] */ protected $resourse; /** * 要操作的資源id * @var [type] */ protected $resourseId; /** * 構(gòu)造函數(shù), 獲取client 請(qǐng)求的方式,以及傳輸?shù)臄?shù)據(jù) * @param object 可以自定義傳入的對(duì)象 */ public function __construct() { //首先對(duì)客戶(hù)端的請(qǐng)求進(jìn)行驗(yàn)證 $this->authorization(); $this->method = strtolower($_SERVER['REQUEST_METHOD']); //所有的請(qǐng)求都是pathinfo模式 $pathinfo = $_SERVER['PATH_INFO']; //將pathinfo數(shù)據(jù)信息映射為實(shí)際請(qǐng)求方法 $this->getResourse($pathinfo); //獲取傳輸?shù)木唧w參數(shù) $this->getData(); //執(zhí)行響應(yīng) $this->doResponse(); } /** * 根據(jù)不同的請(qǐng)求方式,獲取數(shù)據(jù) * @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數(shù)據(jù)信息映射為實(shí)際請(qǐng)求方法 private function getResourse($pathinfo){ /** * 將pathinfo數(shù)據(jù)信息映射為實(shí)際請(qǐng)求方法 * GET /users: 逐頁(yè)列出所有用戶(hù); * POST /users: 創(chuàng)建一個(gè)新用戶(hù); * GET /users/123: 返回用戶(hù)為123的詳細(xì)信息; * PUT /users/123: 更新用戶(hù)123; * DELETE /users/123: 刪除用戶(hù)123; * * 根據(jù)以上規(guī)則,將pathinfo第一個(gè)參數(shù)映射為需要操作的數(shù)據(jù)表, * 第二個(gè)參數(shù)映射為操作的id */ $info = explode('/', ltrim($pathinfo, '/')); list($this->resourse, $this->resourseId) = $info; } /** * 驗(yàn)證請(qǐng)求 */ private function authorization(){ $token = $_SERVER['HTTP_CLIENT_TOKEN']; $authorization = md5(substr(md5($token), 8, 24).$token); if($authorization != $_SERVER['HTTP_CLIENT_CODE']){ //驗(yàn)證失敗,輸出錯(cuò)誤信息給客戶(hù)端 $this->outPut($status = 1); } } /** * [getData 獲取傳送的參數(shù)信息] * @param [type] $pad [description] * @return [type] [description] */ private function getData(){ //所有的參數(shù)都是get傳參 $this->param = $_GET; } /** * 獲取資源操作 * @return [type] [description] */ protected function _get(){ //邏輯代碼根據(jù)自己實(shí)際項(xiàng)目需要實(shí)現(xiàn) } /** * 新增資源操作 * @return [type] [description] */ protected function _post(){ //邏輯代碼根據(jù)自己實(shí)際項(xiàng)目需要實(shí)現(xiàn) } /** * 刪除資源操作 * @return [type] [description] */ protected function _delete(){ //邏輯代碼根據(jù)自己實(shí)際項(xiàng)目需要實(shí)現(xiàn) } /** * 更新資源操作 * @return [type] [description] */ protected function _put(){ //邏輯代碼根據(jù)自己實(shí)際項(xiàng)目需要實(shí)現(xiàn) } /** * 出入服務(wù)端返回的數(shù)據(jù)信息 json格式 */ public function outPut($stat, $data=array()){ $status = array( //0 狀態(tài)表示請(qǐng)求成功 0 => array( 'code' => 1, 'info' => '請(qǐng)求成功', 'data' =>$data ), //驗(yàn)證失敗 1 => array( 'code' => 0, 'info' => '請(qǐng)求不合法' ) ); try{ if(!in_array($stat, array_keys($status))){ throw new Exception('輸入的狀態(tài)碼不合法'); }else{ echo json_encode($status[$stat]); } }catch (Exception $e){ die($e->getMessage()); } } }
ApiClient.php
<?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設(shè)計(jì)指南] */ /*** * * * * * * * * * * * * * * * * * * * * * * * * * ***\ * 定義路由的請(qǐng)求方式 * * * * $url_model=0 * * 采用傳統(tǒng)的URL參數(shù)模式 * * http://serverName/appName/?m=module&a=action&id=1 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * PATHINFO模式(默認(rèn)模式) * * 設(shè)置url_model 為1 * * http://serverName/appName/module/action/id/1/ * ** * * * * * * * * * * * * * * * * * * * * * * * * * * ** */ class restClient { //請(qǐng)求的token const token='yangyulong'; //請(qǐng)求url private $url; //請(qǐng)求的類(lèi)型 private $requestType; //請(qǐng)求的數(shù)據(jù) private $data; //curl實(shí)例 private $curl; public $status; private $headers = array(); /** * [__construct 構(gòu)造方法, 初始化數(shù)據(jù)] * @param [type] $url 請(qǐng)求的服務(wù)器地址 * @param [type] $requestType 發(fā)送請(qǐng)求的方法 * @param [type] $data 發(fā)送的數(shù)據(jù) * @param integer $url_model 路由請(qǐng)求方式 */ 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; } //初始化類(lèi)中的數(shù)據(jù) $this->url = $url; $this->data = $data; try{ if(!$this->curl = curl_init()){ throw new Exception('curl初始化錯(cuò)誤:'); }; }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 設(shè)置get請(qǐng)求的參數(shù)] * @return [type] [description] */ public function _get() { } /** * [_post 設(shè)置post請(qǐng)求的參數(shù)] * post 新增資源 * @return [type] [description] */ public function _post() { curl_setopt($this->curl, CURLOPT_POST, 1); curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->data); } /** * [_put 設(shè)置put請(qǐng)求] * 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 執(zhí)行發(fā)送請(qǐng)求] * @return [type] [description] */ public function doRequest() { //發(fā)送給服務(wù)端驗(yàn)證信息 if((null !== self::token) && self::token){ $this->headers = array( 'Client_Token: '.self::token, 'Client_Code: '.$this->setAuthorization() ); } //發(fā)送頭部信息 $this->setHeader(); //發(fā)送請(qǐng)求方式 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; } //執(zhí)行curl請(qǐng)求 $info = curl_exec($this->curl); //獲取curl執(zhí)行狀態(tài)信息 $this->status = $this->getInfo(); return $info; } /** * 設(shè)置發(fā)送的頭部信息 */ private function setHeader(){ curl_setopt($this->curl, CURLOPT_HTTPHEADER, $this->headers); } /** * 生成授權(quán)碼 * @return string 授權(quán)碼 */ private function setAuthorization(){ $authorization = md5(substr(md5(self::token), 8, 24).self::token); return $authorization; } /** * 獲取curl中的狀態(tài)信息 */ public function getInfo(){ return curl_getinfo($this->curl); } /** * 關(guān)閉curl連接 */ public function __destruct(){ curl_close($this->curl); } }
testClient.php
<?php /** * Created by PhpStorm. * User: anziguoer@sina.com * Date: 2015/4/29 * Time: 12:35 */ include './ApiClient.php'; $arr = array( 'user' => 'anziguoer', 'passwd' => 'yangyulong' ); // $url = 'http://localhost/restAPI/restServer.php'; $url = 'http://localhost/restAPI/testServer.php/user/123'; $rest = new restClient($url, $arr, 'get'); $info = $rest->doRequest(); //獲取curl中的狀態(tài)信息 $status = $rest->status; echo '<pre>'; print_r($info); echo '</pre>';
testServer.php
<?php /** * @Author: anziguoer@sina.com * @Email: anziguoer@sina.com * @link: https://git.oschina.net/anziguoer * @Date: 2015-04-30 16:52:53 * @Last Modified by: yangyulong * @Last Modified time: 2015-04-30 17:26:37 */ include './ApiServer.php'; class testServer extends apiServer { /** * 先執(zhí)行apiServer中的方法,初始化數(shù)據(jù) * @param object $obj 可以傳入的全局對(duì)象[數(shù)據(jù)庫(kù)對(duì)象,框架全局對(duì)象等] */ private $obj; function __construct()//object $obj { parent::__construct(); //$this->obj = $obj; //$this->resourse; 父類(lèi)中已經(jīng)實(shí)現(xiàn),此類(lèi)中可以直接使用 //$tihs->resourseId; 父類(lèi)中已經(jīng)實(shí)現(xiàn),此類(lèi)中可以直接使用 } /** * 獲取資源操作 * @return [type] [description] */ protected function _get(){ echo "get"; //邏輯代碼根據(jù)自己實(shí)際項(xiàng)目需要實(shí)現(xiàn) } /** * 新增資源操作 * @return [type] [description] */ protected function _post(){ echo "post"; //邏輯代碼根據(jù)自己實(shí)際項(xiàng)目需要實(shí)現(xiàn) } /** * 刪除資源操作 * @return [type] [description] */ protected function _delete(){ //邏輯代碼根據(jù)自己實(shí)際項(xiàng)目需要實(shí)現(xiàn) } /** * 更新資源操作 * @return [type] [description] */ protected function _put(){ echo "put"; //邏輯代碼根據(jù)自己實(shí)際項(xiàng)目需要實(shí)現(xiàn) } } $server = new testServer();
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
相關(guān)文章
php微信公眾號(hào)開(kāi)發(fā)之校園圖書(shū)館
這篇文章主要為大家詳細(xì)介紹了php微信公眾號(hào)開(kāi)發(fā)之校園圖書(shū)館,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10laravel validate 設(shè)置為中文的例子(驗(yàn)證提示為中文)
今天小編就為大家分享一篇laravel validate 設(shè)置為中文的例子(驗(yàn)證提示為中文),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09實(shí)例介紹PHP中zip_open()函數(shù)用法
在本篇內(nèi)容里小編給大家分享了關(guān)于PHP中zip_open()函數(shù)用法的相關(guān)知識(shí)點(diǎn),有需要的朋友們跟著學(xué)習(xí)下。2019-02-02基于PHP實(shí)現(xiàn)簡(jiǎn)單的隨機(jī)抽獎(jiǎng)小程序
一個(gè)抽獎(jiǎng)小程序,概論可控,也可某個(gè)獎(jiǎng)品在前端顯示,而程序中根本不可能獲得!把所有的概率x10后相加起來(lái),新數(shù)組中每項(xiàng)的值等于它前幾個(gè)的和加上它本身2016-01-01PHP將數(shù)據(jù)導(dǎo)出Excel表中的實(shí)例(投機(jī)型)
下面小編就為大家?guī)?lái)一篇PHP將數(shù)據(jù)導(dǎo)出Excel表中的實(shí)例(投機(jī)型)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07基于PHP代碼實(shí)現(xiàn)中獎(jiǎng)概率算法可用于刮刮卡、大轉(zhuǎn)盤(pán)等抽獎(jiǎng)算法
大轉(zhuǎn)盤(pán)中獎(jiǎng)概率算法在我們的日常生活中,經(jīng)常遇到,那么基于php代碼是如何實(shí)現(xiàn)中獎(jiǎng)概率算法的,下面通過(guò)一段代碼實(shí)例給大家介紹php中獎(jiǎng)概率算法2015-12-12微信公眾號(hào)之主動(dòng)給用戶(hù)發(fā)送消息功能
這篇文章主要介紹了微信公眾號(hào)之主動(dòng)給用戶(hù)發(fā)送消息,需要的朋友可以參考下2019-06-06php正確輸出json數(shù)據(jù)的實(shí)例講解
今天小編就為大家分享一篇php正確輸出json數(shù)據(jù)的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08php二維數(shù)組用鍵名分組相加實(shí)例函數(shù)
php二維數(shù)組以鍵名進(jìn)行分組相加的實(shí)例程序2013-11-11