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

首頁 > 語言 > PHP > 正文

php curl操作API接口類完整示例

2024-05-05 00:09:09
字體:
來源:轉載
供稿:網友

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

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

希望本文所述對大家PHP程序設計有所幫助。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 扎赉特旗| 文水县| 漳浦县| 镇康县| 丰原市| 桐梓县| 本溪市| 玛多县| 菏泽市| 德庆县| 桃江县| 郑州市| 女性| 拉萨市| 靖远县| 许昌市| 昭平县| 昌乐县| 商都县| 宜丰县| 顺义区| 舒城县| 揭东县| 米泉市| 普兰店市| 临湘市| 天等县| 淮南市| 弋阳县| 建阳市| 莲花县| 屯留县| 陕西省| 垫江县| 厦门市| 林芝县| 南平市| 定安县| 石屏县| 梅州市| 宣汉县|