欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php基于curl擴(kuò)展制作跨平臺(tái)的restfule 接口

 更新時(shí)間:2015年05月11日 11:49:51   投稿:hebedich  
這篇文章主要介紹了php基于curl擴(kuò)展制作跨平臺(tái)的restfule 接口的相關(guān)資料以及詳細(xì)的代碼,有需要的小伙伴可以參考下。

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)文章

最新評(píng)論