class HttpCurl
{
//控客雲平台的appid
private $appId = xxxxxx;
//控客雲平台的appkey
private $appKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx';
//控客接口請求地址
protected $url = 'https://xxxxxxxxxxx/1.0/';
//接口請求要求host必須為api.developer.hijaytech.com
protected $host = 'api.developer.hijaytech.com';
//設置的接口請求頭信息集合
protected $headerArr = '';
protected $pragma = 'no-cache';
protected $accept = 'application/json';
protected $contentType = 'application/json';
public function __construct()
{
$this->setHeader();
}
/**
* 設置請求接口的頭信息,開發文檔中的一些固定的頭信息參數
*/
protected function setHeader()
{
$arr = array(
'Host:' . $this->host,
'Pragma:' . $this->pragma,
'Accept:' . $this->accept,
'Content-Type:' . $this->contentType,
'appId:' . $this->appId,
'appKey:' . $this->appKey,
);
$this->headerArr = $arr;
}
/**
* 接口的請求方法
* @param string $url 接口的具體訪問地址
* @param array $data 請求數據 默認是get方式請求。為空數組
* @param string $method 接口的請求方式post,get,delete,put
* @param array $header 接口的請求頭,拓展,在默認固定的基礎上可以再添加
* @return array 數據結果集合array(
* 'res' => $res, //請求狀態是失敗還是成功
* 'mes' => $mes, //請求失敗的錯誤信息
* 'data' => $data //請求失敗對應的數據
* );
*/
protected function curlRequest($url, $data = array(), $method = 'post', $header = array())
{
//初始化curl_init()
$ch = curl_init();
//設置請求的url
curl_setopt($ch, CURLOPT_URL, $url);
//設置是否接受返回結果
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 顯示返回的Header區域內容
curl_setopt($ch, CURLOPT_HEADER, false);
//設置端口號
curl_setopt($ch, CURLOPT_PORT, 4432);
//https請求時會要求驗證證書(ssl證書)
// $sslPath = '';
// $ssl = substr($url, 0, 8) == 'https://' ? true : false;
// if($ssl){
// curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,true);
// curl_setopt($ch,CURLOPT_CAINFO,$sslPath);
// curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);
// }else{
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //這個是重點,跳過證書
// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); // 從證書中檢查SSL加密算法是否存在
// }
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //這個是重點,跳過證書
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 從證書中檢查SSL加密算法是否存在
//根據不同的請求方式設置對應的參數
switch ($method) {
case 'get':
curl_setopt($ch, CURLOPT_HTTPGET, true);
break;
case 'post':
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
break;
case 'delete':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
case 'put':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
break;
default :
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
break;
}
//自定義請求頭信息
if (count($header) <= 0) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headerArr);
} else {
$headers = $this->headerArr;
$headerArr = array_merge($headers, $header);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
}
//返回一個json數據
$result = curl_exec($ch);
if (curl_error($ch)) {
$resArr = $this->returnRes('fail', curl_error($ch), $data);
curl_close($ch);
return $resArr;
}
curl_close($ch);
//將返回數據格式化為數組形式
$resData = json_decode($result, true);
if(isset($resData['error_code'])&&$resData['error_code']!==0){
$resArr = $this->returnRes('fail', $resData['error_msg'], $data);
return $resArr;
}
//統一返回的數據格式
$resArr = $this->returnRes('success', '', $resData);
return $resArr;
}
public function returnRes($res, $mes, $data)
{
return array(
'res' => $res, //請求狀態是失敗還是成功
'mes' => $mes, //請求失敗的錯誤信息
'data' => $data //請求失敗對應的數據
);
}
}