You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
6.4 KiB

<?php
namespace LaneWeChat\Core;
/**
* CURL工具类用于简化HTTP请求操作。
* Class Curl
* Created by Lane.
* @Author: lane
* @Mail: lixuan868686@163.com
* @Date: 14-1-10
* @Time: 下午4:22
* Mail: lixuan868686@163.com
* Website: http://www.lanecn.com
*/
class Curl
{
// 私有静态变量用于存储CURL会话句柄、头部、主体等信息
private static $_ch;
private static $_header;
private static $_body;
// 存储Cookie、选项、URL和Referer的数组
private static $_cookie = array();
private static $_options = array();
private static $_url = array();
private static $_referer = array();
/**
* 调用外部URL支持GET和POST请求。
* @param string $queryUrl 请求的URL
* @param array|string $param 参数
* @param string $method 请求方法,默认为'get'
* @return bool|mixed 返回请求结果或false
*/
public static function callWebServer($queryUrl, $param = '', $method = 'get', $is_json = true, $is_urlcode = true)
{
// 检查URL是否为空
if (empty($queryUrl)) {
return false;
}
// 转换方法为小写
$method = strtolower($method);
$ret = '';
// 确保参数是数组
$param = empty($param) ? array() : $param;
// 初始化CURL会话
self::_init();
// 根据请求方法调用相应的私有方法
if ($method == 'get') {
$ret = self::_httpGet($queryUrl, $param);
} elseif ($method == 'post') {
$ret = self::_httpPost($queryUrl, $param, $is_urlcode);
}
// 如果有返回结果根据参数决定是否解析为JSON
if (!empty($ret)) {
if ($is_json) {
return json_decode($ret, true);
} else {
return $ret;
}
}
return false;
}
/**
* 初始化CURL会话
*/
private static function _init()
{
self::$_ch = curl_init();
// 设置CURL选项
curl_setopt(self::$_ch, CURLOPT_HEADER, true);
curl_setopt(self::$_ch, CURLOPT_RETURNTRANSFER, true);
}
/**
* 设置CURL选项
* @param array $optArray 选项数组
*/
public static function setOption($optArray = array())
{
foreach ($optArray as $opt) {
curl_setopt(self::$_ch, $opt['key'], $opt['value']);
}
}
/**
* 关闭CURL会话
* @return bool
*/
private static function _close()
{
if (is_resource(self::$_ch)) {
curl_close(self::$_ch);
}
return true;
}
/**
* 发送GET请求
* @param string $url 请求URL
* @param array $query 查询参数
* @return mixed 请求结果
*/
private static function _httpGet($url, $query = array())
{
// 构建查询字符串
if (!empty($query)) {
$url .= (strpos($url, '?') === false) ? '?' : '&';
$url .= is_array($query) ? http_build_query($query) : $query;
}
// 设置CURL选项
curl_setopt(self::$_ch, CURLOPT_URL, $url);
curl_setopt(self::$_ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt(self::$_ch, CURLOPT_HEADER, 0);
// 禁用SSL证书验证
curl_setopt(self::$_ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt(self::$_ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt(self::$_ch, CURLOPT_SSLVERSION, 1);
// 执行请求并返回结果
$ret = self::_execute();
self::_close();
return $ret;
}
/**
* 发送POST请求
* @param string $url 请求URL
* @param array $query 查询参数
* @param bool $is_urlcode 是否进行URL编码
* @return mixed 请求结果
*/
private static function _httpPost($url, $query = array(), $is_urlcode = true)
{
// 对参数进行URL编码
if (is_array($query)) {
foreach ($query as $key => $val) {
if ($is_urlcode) {
$encode_key = urlencode($key);
} else {
$encode_key = $key;
}
if ($encode_key != $key) {
unset($query[$key]);
}
if ($is_urlcode) {
$query[$encode_key] = urlencode($val);
} else {
$query[$encode_key] = $val;
}
}
}
// 设置CURL选项
curl_setopt(self::$_ch, CURLOPT_URL, $url);
curl_setopt(self::$_ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt(self::$_ch, CURLOPT_HEADER, 0);
curl_setopt(self::$_ch, CURLOPT_POST, true);
curl_setopt(self::$_ch, CURLOPT_POSTFIELDS, $query);
// 禁用SSL证书验证
curl_setopt(self::$_ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt(self::$_ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt(self::$_ch, CURLOPT_SSLVERSION, 1);
// 执行请求并返回结果
$ret = self::_execute();
self::_close();
return $ret;
}
/**
* 发送PUT请求
* @param string $url 请求URL
* @param array $query 查询参数
* @return mixed 请求结果
*/
private static function _put($url, $query = array())
{
curl_setopt(self::$_ch, CURLOPT_CUSTOMREQUEST, 'PUT');
return self::_httpPost($url, $query);
}
/**
* 发送DELETE请求
* @param string $url 请求URL
* @param array $query 查询参数
* @return mixed 请求结果
*/
private static function _delete($url, $query = array())
{
curl_setopt(self::$_ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
return self::_httpPost($url, $query);
}
/**
* 发送HEAD请求
* @param string $url 请求URL
* @param array $query 查询参数
* @return mixed 请求结果
*/
private static function _head($url, $query = array())
{
curl_setopt(self::$_ch, CURLOPT_CUSTOMREQUEST, 'HEAD');
return self::_httpPost($url, $query);
}
/**
* 执行CURL请求
* @return mixed 请求结果
*/
private static function _execute()
{
$response = curl_exec(self::$_ch);
$errno = curl_errno(self::$_ch);
// 如果发生错误,抛出异常
if ($errno > 0) {
throw new \Exception(curl_error(self::$_ch), $errno);
}
return $response;
}
}