/**
* @var array 设置http头部信息
*/
public $httpHeader = ["Content-Type:application/json"];
/**
* @var int $timeout 超时时间
*/
protected $timeout = 10;
/**
* @var mixed curl域名地址
*/
protected $curlDomain;
/**
* @var string 请求参数格式
*/
public $format = self::FORMAT_RAW;
const FORMAT_RAW = 'raw';
const FORMAT_HTML = 'html';
const FORMAT_JSON = 'json';
const FORMAT_JSONP = 'jsonp';
const FORMAT_XML = 'xml';
/**
* get请求
* @param $url
* @param $params
* @return bool|mixed
*/
public function curl_get($url, $params)
{
if (!is_string($url) || empty($url) || !is_string('get')) return false;
self::filter_url($url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->httpHeader);//定义header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($params && !empty($params)) {
$param = '?';
foreach ($params as $k => $v) {
$param .= $k . '=' . $v . '&&';
}
$param = substr($param, 0, strlen($param) - 2);
$url = $url . $param;
}
curl_setopt($ch, CURLOPT_URL, $url);
//debug调试模式下不设置请求超时时间
if (config('app_debug')) {
curl_setopt($ch, CURLOPT_TIMEOUT, 0xFFFFfff);
} else {
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
}
$result = curl_exec($ch);
$errors = curl_error($ch);
curl_close($ch);
if (!empty($errors)) {
exit(ajaxErr(1001));
}
return json_decode($result);
}
/**
* post请求
* @param $url
* @param $params
* @return bool|mixed
*/
public function curl_post($url, $params)
{
if (!is_string($url) || empty($url) || !is_string('post')) return false;
self::filter_url($url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->httpHeader);//定义header
curl_setopt($ch, CURLOPT_POST, 1); //定义提交类型 1:POST ;0:GET
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($params && !empty($params)) {
if ($this->format == self::FORMAT_RAW)
$params = json_encode($params);
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);//定义提交的数据
curl_setopt($ch, CURLOPT_URL, $url);
//debug调试模式下不设置请求超时时间
if (config('app_debug')) {
curl_setopt($ch, CURLOPT_TIMEOUT, 0xFFFFfff);
} else {
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
}
$result = curl_exec($ch);
$errors = curl_error($ch);
curl_close($ch);
if (!empty($errors)) {
exit(ajaxErr(1001));
}
return json_decode($result);
}
/**
* put请求
* @param $url
* @param $params
* @return bool|mixed
*/
public function curl_put($url, $params)
{
if (!is_string($url) || empty($url) || !is_string('put')) return false;
self::filter_url($url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->httpHeader);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "put");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($params && !empty($params)) {
if ($this->format == self::FORMAT_RAW)
$params = json_encode($params);
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);//定义提交的数据
curl_setopt($ch, CURLOPT_URL, $url);
//debug调试模式下不设置请求超时时间
if (config('app_debug')) {
curl_setopt($ch, CURLOPT_TIMEOUT, 0xFFFFfff);
} else {
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
}
$result = curl_exec($ch);
$errors = curl_error($ch);
curl_close($ch);
if (!empty($errors)) {
exit(ajaxErr(1001));
}
return json_decode($result);
}
/**
* delete请求
* @param $url
* @param $params
* @return bool|mixed
*/
public function curl_delete($url, $params)
{
if (!is_string($url) || empty($url) || !is_string('delete')) return false;
self::filter_url($url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->httpHeader);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "delete");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($params && !empty($params)) {
$param = '?';
foreach ($params as $k => $v) {
$param .= $k . '=' . $v . '&&';
}
$param = substr($param, 0, strlen($param) - 2);
$url = $url . $param;
}
curl_setopt($ch, CURLOPT_URL, $url);
//debug调试模式下不设置请求超时时间
if (config('app_debug')) {
curl_setopt($ch, CURLOPT_TIMEOUT, 0xFFFFfff);
} else {
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
}
$result = curl_exec($ch);
$errors = curl_error($ch);
curl_close($ch);
if (!empty($errors)) {
exit(ajaxErr(1001));
}
return json_decode($result);
}
/**
* 请求url地址前缀过滤
* Author: huxinlu
* @param $url
*/
private function filter_url(&$url)
{
if (substr($url, 0, 5) !== 'http:' && substr($url, 0, 6) !== 'https:') {
if (substr($url, 0, 1) == '/') {
$url = substr($url, 1, strlen($url) - 1);
}
$url = $this->curlDomain . $url;
}
}
curl请求
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- //curl访问//需要url或者data//返回的数组是JSON数据形式function ppd_curl($u...
- 此种情况多发生在自签名的证书,报错含义是签发证书机构未经认证,无法识别。解决办法就是替换证书ca-bundle.c...
- 今天发现请求方提供的curl语句,认证不通过,该认证信息在url中query params进行传递(用&进行连接)...