10、CURL类封装
在对接API接口中,我们不可能每次都重新编写CURL系列代码,
肯定是会把CURL封装成一个自定义函数,
但由于CURL的环境参数多变,我们不可能设计一个传递很多参数的函数,然后中间嵌套数十个IF判断。
CURL的封装应该使用Class类,用成员属性可以控制参数,用于成员方法来进行逻辑解耦。
<?php
class Curl{
/**
* API请求地址
*/
public $apiUrl;
/**
* API请求参数
*/
public $apiData;
/**
* API请求头信息
*/
public $apiHeaders;
/**
* HTTP动词
*/
public $apiHavior = 'GET';
/**
* 请求最长等待时间(秒)
*/
public $apiTime = 60;
/**
* 是否使用POST
*/
public $apiPost = false;
/**
* 是否开启debug模式
*/
public $apiDebug = false;
/**
* 是否开启debug模式
*/
public $DebugLog = [];
/**
* CURL连接句柄
*/
private $_curl;
/**
* 发送请求
* @return
*/
public function exec() {
# 检测参数
$this->issetName();
# 初始化连接句柄
$this->_curl = curl_init();
# 设置基本请求信息
$this->set_basicHead();
# 设置重要的请求信息
$this->set_vitalHead();
if ($this->apiDebug == true) { $this->DebugLog[] = '开始发送请求...'; }
# 发送CURL
$res = curl_exec($this->_curl);
if ($res === false) {
if ($this->apiDebug == true) { $this->DebugLog[] = '请求失败...'; }
return $this->Debug('API请求失败,原因:'.curl_error($this->_curl));
}
if ($this->apiDebug == true) { $this->DebugLog[] = '请求成功...out'; }
# 释放curl句柄
curl_close($this->_curl);
# 返回请求结果
return $res;
}
/**
* 检测各项必填参数
*
* @return bool
*/
private function issetName() {
if ($this->apiDebug == true) { $this->DebugLog[] = '开始检测参数...'; }
if (empty($this->apiUrl)) { return $this->Debug('API请求地址为空');}
if (empty($this->apiHavior)) { return $this->Debug('API请求动词为空');}
if ($this->apiDebug == true) { $this->DebugLog[] = '开始检测通过...OK'; }
return true;
}
/**
* 用于Debug下输出内容还是返回bool
*
* @param string $msg Debug模式下记录错误提示
* @return bool false
*/
private function Debug($msg=null) {
if ($this->apiDebug == true) {
if ($this->apiDebug == true) { $this->DebugLog[] = $msg; }
}
return false;
}
/**
* 设置基本请求信息
*/
private function set_basicHead() {
if ($this->apiDebug == true) { $this->DebugLog[] = '设置基本请求参数...'; }
# 设置API请求地址
curl_setopt($this->_curl, CURLOPT_URL, $this->apiUrl);
# 关闭请求成功后自动输出的设置
curl_setopt($this->_curl, CURLOPT_RETURNTRANSFER, true);
# 关闭回调内容中带请求头信息
curl_setopt($this->_curl, CURLOPT_HEADER, false);
# 设置最大请求等待时间为60秒
curl_setopt($this->_curl, CURLOPT_TIMEOUT, $this->apiTime);
# 设置请求HTTP动词
curl_setopt($this->_curl, CURLOPT_CUSTOMREQUEST, $this->apiHavior);
# 设置请求类型为POST
if ($this->apiPost) {
curl_setopt($this->_curl, CURLOPT_POST, true);
if ($this->apiDebug == true) { $this->DebugLog[] = '1、开启POST请求...OK'; }
}
# 自动检测https请求
if (strripos($this->apiUrl, 'https') !== false) {
curl_setopt($this->_curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($this->_curl, CURLOPT_SSL_VERIFYSTATUS, false);
if ($this->apiDebug == true) { $this->DebugLog[] = '2、开启HTTPS请求协议...OK'; }
}
if ($this->apiDebug == true) { $this->DebugLog[] = '基本请求参数设置完成...'; }
}
/**
* 设置重要的请求信息
*/
private function set_vitalHead() {
if ($this->apiDebug == true) { $this->DebugLog[] = '设置重要请求参数...'; }
# 设置请求参数
if (!empty($this->apiData)) {
curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $this->apiData);
if ($this->apiDebug == true) { $this->DebugLog[] = '1、设置请求内容...OK'; }
}
# 设置请求头
if (!empty($this->apiHeaders)) {
curl_setopt($this->_curl, CURLOPT_HTTPHEADER, $this->apiHeaders);
if ($this->apiDebug == true) { $this->DebugLog[] = '2、设置请求头...OK'; }
}
if ($this->apiDebug == true) { $this->DebugLog[] = '重要请求参数设置完成...'; }
}
}
# 使用demo
$curl = new Curl();
$curl->apiDebug = true; // 开启调试模式
$curl->apiUrl = 'https://www.baidu.com/11';
$res = $curl->exec();
echo '<pre>';
var_dump($res); // 打印返回值
var_dump($curl->DebugLog); // 打印LOG