curl请求的get.post.put.delete对接其他系统接口方法

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 //请求失败对应的数据
);
}


}
posted @ 2018-05-15 16:37  ~~菜鸟中的战斗机  阅读(1190)  评论(0编辑  收藏  举报