本文共计540个文字,预计阅读时间需要3分钟。

封装了简单的Curl操作 // 技术支持:http://evalor.cn/----------------------------------------------------------namespace data\plugins;/** * 封装Curl处理类 * Class GoCurl * @author evalor * @package data\plugins\GoCurl */
封装了简易的Curl操作
// | Technical support: evalor.cn
//----------------------------------------------------------
namespace data\plugins;
/**
* 封装Curl处理类
* Class GoCurl
* @author : evalor
* @package data\plugins\GoCurl
*/
class GoCurl
{
protected $_instance;
protected $_options;
protected $_error;
/**
* 构建curl实例
* GoCurl constructor.
* @param array $options
* @author : evalor
*/ public function __construct($options = '') { $this->_instance = curl_init(); // 默认应用配置 $this->_options = [ CURLOPT_AUTOREFERER => true, // 跳转时带上上一个跳转的来路 CURLOPT_FOLLOWLOCATION => true, // 自动跟随跳转 CURLOPT_RETURNTRANSFER => true, // 以字符串返回请求的内容 ]; if (is_array($options)) { // 如果传入的是数组则合并到配置 $this->setMultiOptions($options); } else { // 如果传入的是字符串则设置链接 $this->setUrl($options); } } /** * 销毁curl实例 * oCurl destructor. * @author : evalor
*/ public function __destruct() { curl_close($this->_instance); } /** * 批量设置当前请求实例的Options * @param array $Options 需要批量配置的参数 * @author : evalor
*/ public function setMultiOptions($Options) { if (!empty($Options)) $this->_options = array_merge($this->_options, $Options); } /** * 设置不获取返回内容的Body * @author : evalor
* @return $this */ public function setNoBody() { $this->_options [CURLOPT_NOBODY] = true; return $this; } /** * 返回请求的原生报文 * @author : evalor
* @return $this */ public function setRaw() { $this->_options [CURLOPT_BINARYTRANSFER] = true; return $this; } /** * 设置关闭SSL验证(用于访问HTTPS链接) * @author : evalor
* @return $this */ public function setCloseSSL() { $this->_options [CURLOPT_SSL_VERIFYHOST] = false; $this->_options [CURLOPT_SSL_VERIFYHOST] = false; return $this; } /** * 设置Post信息 * @param $postData * @author : evalor
* @return $this */ public function setPostData($postData) { $this->_options [CURLOPT_POST] = false; $this->_options [CURLOPT_POSTFIELDS] = $postData; return $this; } /** * 设置请求的Url * @param string $Url 需要发送请求的url地址 * @author : evalor
* @return $this */ public function setUrl($Url) { $this->_options [CURLOPT_URL] = $Url; return $this; } /** * 执行Curl请求 * @author : evalor
* @param bool $reArray 是否自动进行Json解码 * @return mixed */ public function Send($reArray = true) { // 设置本次请求的配置 curl_setopt_array($this->_instance, $this->_options); $retval = curl_exec($this->_instance); if ($retval === false) { // 请求失败的处理 $this->_error = curl_error($this->_instance); return false; } return $reArray ? json_decode($retval, true) : $retval; } /** * 获取错误信息 * @author : evalor
* @return mixed */ public function getLastError() { return $this->_error; } }
