校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁(yè) > 知識(shí)庫(kù) > php curl操作API接口類(lèi)完整示例

php curl操作API接口類(lèi)完整示例

熱門(mén)標(biāo)簽:開(kāi)發(fā)地圖標(biāo)注類(lèi)網(wǎng)站 電銷(xiāo)機(jī)器人問(wèn)門(mén)薩維品牌my 百度地圖標(biāo)注偏差 百度地圖怎樣標(biāo)注圖標(biāo) 外呼系統(tǒng)能給企業(yè)帶來(lái)哪些好處 400電話(huà)蘭州申請(qǐng)請(qǐng) 咸寧銷(xiāo)售電銷(xiāo)機(jī)器人系統(tǒng) 余姚電話(huà)機(jī)器人 廣東廣州在怎么申請(qǐng)400電話(huà)

本文實(shí)例講述了php curl操作API接口類(lèi)。分享給大家供大家參考,具體如下:

?php
namespace curl;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/16
 * Time: 9:54
 */
class ApiClient
{
//請(qǐng)求的token
 const token='token_str';
 //請(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);
  //curl_setopt($this->curl, CURLOPT_HEADER, 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 json_decode($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);
 }
}

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《php curl用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》及《PHP中json格式數(shù)據(jù)操作技巧匯總》

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • PHP開(kāi)發(fā)api接口安全驗(yàn)證操作實(shí)例詳解
  • PHP開(kāi)發(fā)api接口安全驗(yàn)證的實(shí)例講解
  • PHP API接口必備之輸出json格式數(shù)據(jù)示例代碼
  • PHP實(shí)現(xiàn)的同步推薦操作API接口案例分析
  • PHP微信API接口類(lèi)
  • PHP微信紅包API接口
  • PHP下使用CURL方式POST數(shù)據(jù)至API接口的代碼
  • PHP本地進(jìn)行API接口測(cè)試的實(shí)例
  • 支付寶服務(wù)窗API接口開(kāi)發(fā)php版本
  • PHP如何使用JWT做Api接口身份認(rèn)證的實(shí)現(xiàn)
  • PHP開(kāi)發(fā)API接口簽名生成及驗(yàn)證操作示例

標(biāo)簽:重慶 十堰 麗江 銅陵 鷹潭 巴彥淖爾 臨沂 衡陽(yáng)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《php curl操作API接口類(lèi)完整示例》,本文關(guān)鍵詞  php,curl,操作,API,接口,類(lèi),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《php curl操作API接口類(lèi)完整示例》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于php curl操作API接口類(lèi)完整示例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 招远市| 东乌珠穆沁旗| 泰州市| 平原县| 南陵县| 昌乐县| 武夷山市| 平罗县| 平阴县| 林芝县| 威海市| 安阳县| 隆回县| 神木县| 桓仁| 河西区| 和硕县| 柞水县| 江油市| 申扎县| 南京市| 那曲县| 运城市| 神农架林区| 集贤县| 噶尔县| 衡东县| 贵州省| 甘孜县| 阿城市| 益阳市| 新野县| 南漳县| 高密市| 将乐县| 元江| 简阳市| 桦川县| 北碚区| 麟游县| 抚远县|