php curl操作API接口類完整示例
本文實例講述了php curl操作API接口類。分享給大家供大家參考,具體如下:
<?php
namespace 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;
//請求的數(shù)據(jù)
private $data;
//curl實例
private $curl;
public $status;
private $headers = array();
/**
* [__construct 構(gòu)造方法, 初始化數(shù)據(jù)]
* @param [type] $url 請求的服務(wù)器地址
* @param [type] $requestType 發(fā)送請求的方法
* @param [type] $data 發(fā)送的數(shù)據(jù)
* @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;
}
//初始化類中的數(shù)據(jù)
$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 設(shè)置get請求的參數(shù)]
* @return [type] [description]
*/
public function _get() {
}
/**
* [_post 設(shè)置post請求的參數(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請求]
* 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ā)送請求]
* @return [type] [description]
*/
public function doRequest() {
//發(fā)送給服務(wù)端驗證信息
if((null !== self::token) && self::token){
$this->headers = array(
'Client-Token:'.self::token,//此處不能用下劃線
'Client-Code:'.$this->setAuthorization()
);
}
//發(fā)送頭部信息
$this->setHeader();
//發(fā)送請求方式
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請求
$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)容感興趣的讀者可查看本站專題:《php curl用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》及《PHP中json格式數(shù)據(jù)操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
php實現(xiàn)utf-8和GB2312編碼相互轉(zhuǎn)換函數(shù)代碼
php實現(xiàn)utf-8和GB2312編碼相互轉(zhuǎn)換的一個函數(shù),有需要的朋友可以參考下2013-02-02
PHP錯誤Warning:mysql_query()解決方法
這篇文章主要介紹了PHP錯誤Warning:mysql_query()的解決方法,希望可以真正解決大家的問題,需要的朋友可以參考下2015-10-10
使用cookie實現(xiàn)統(tǒng)計訪問者登陸次數(shù)
本篇文章是對使用cookie實現(xiàn)統(tǒng)計訪問者登陸次數(shù)的代碼進行了詳細的分析介紹,需要的朋友參考下2013-06-06
PHP學(xué)習(xí)之?dāng)?shù)組的定義和填充
先了解一下數(shù)組,數(shù)組就是把一組數(shù)據(jù)按順序放在一起。PHP的數(shù)組和其它的語言數(shù)組有一點點不同:第一,保存的數(shù)據(jù)是可以是任何類型的;第二,數(shù)組的索引可以是數(shù)字,也可以是字符串。2011-04-04

