Posted in PHP onApril 01, 2021
GET案例
/**
* curl_get
* @param $url
* @param null $param
* @param null $options
* @return array
*/
function curl_get($url,$param = null,$options = null){
if(empty($options)){
$options = array(
'timeout' => 30,// 请求超时
'header' => array(),// 数据格式如array('Accept: */*','Accept-Encoding: gzip, deflate, br');直接复制浏览器请求头改成一维数组即可
'cookie' => '',// cookie字符串,浏览器直接复制即可
'cookie_file' => '',// 文件路径,并要有读写权限的
'ssl' => 0,// 是否检查https协议
'referer' => null
);
}else{
empty($options['timeout']) && $options['timeout'] = 30;
empty($options['ssl']) && $options['ssl'] = 0;
}
$result = array(
'code' => 0,
'msg' => 'success',
'body' => ''
);
if(is_array($param)){
$param = http_build_query($param);
}
$url = strstr($url,'?')?trim($url,'&').'&'.$param:$url.'?'.$param;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);// 设置url
!empty($options['header']) && curl_setopt($ch, CURLOPT_HTTPHEADER, $options['header']); // 设置请求头
if(!empty($options['cookie_file']) && file_exists($options['cookie_file'])){
curl_setopt($ch, CURLOPT_COOKIEFILE, $options['cookie_file']);
curl_setopt($ch, CURLOPT_COOKIEJAR, $options['cookie_file']);
}else if(!empty($options['cookie'])){
curl_setopt($ch, CURLOPT_COOKIE, $options['cookie']);
}
curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); //curl解压gzip页面内容
curl_setopt($ch, CURLOPT_HEADER, 0);// 不获取请求头
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// 输出转移,不输出页面
!$options['ssl'] && curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $options['ssl']); // 禁止服务器端的验证ssl
!empty($options['referer']) && curl_setopt($ch, CURLOPT_REFERER, $options['referer']);//伪装请求来源,绕过防盗
curl_setopt($ch, CURLOPT_TIMEOUT, $options['timeout']);
//执行并获取内容
$output = curl_exec($ch);
//对获取到的内容进行操作
if($output === FALSE ){
$result['code'] = 1; // 错误
$result['msg'] = "CURL Error:".curl_error($ch);
}
$result['body'] = $output;
//释放curl句柄
curl_close($ch);
return $result;
}
POST案例
/**
* curl_post
* @param $url 请求地址
* @param null $param get参数
* @param array $options 配置参数
* @return array
*/
function curl_post($url,$param = null,$options = array()){
if(empty($options)){
$options = array(
'timeout' => 30,
'header' => array(),
'cookie' => '',
'cookie_file' => '',
'ssl' => 0,
'referer' => null
);
}else{
empty($options['timeout']) && $options['timeout'] = 30;
empty($options['ssl']) && $options['ssl'] = 0;
}
$result = array(
'code' => 0,
'msg' => 'success',
'body' => ''
);
if(is_array($param)){
$param = http_build_query($param);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);// 设置url
!empty($options['header']) && curl_setopt($ch, CURLOPT_HTTPHEADER, $options['header']); // 设置请求头
if(!empty($options['cookie_file']) && file_exists($options['cookie_file'])){
curl_setopt($ch, CURLOPT_COOKIEFILE, $options['cookie_file']);
curl_setopt($ch, CURLOPT_COOKIEJAR, $options['cookie_file']);
}else if(!empty($options['cookie'])){
curl_setopt($ch, CURLOPT_COOKIE, $options['cookie']);
}
curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); //curl解压gzip页面内容
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
curl_setopt($ch, CURLOPT_HEADER, 0);// 不获取请求头
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// 输出转移,不输出页面
!$options['ssl'] && curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $options['ssl']); // 禁止服务器端的验证ssl
!empty($options['referer']) && curl_setopt($ch, CURLOPT_REFERER, $options['referer']);//伪装请求来源,绕过防盗
curl_setopt($ch, CURLOPT_TIMEOUT, $options['timeout']);
//执行并获取内容
$output = curl_exec($ch);
//对获取到的内容进行操作
if($output === FALSE ){
$result['code'] = 1; // 错误
$result['msg'] = "CURL Error:".curl_error($ch);
}
$result['body'] = $output;
//释放curl句柄
curl_close($ch);
return $result;
}
其他请求类型请自己参考封装处理
php远程请求CURL案例(爬虫、保存登录状态)
- Author -
slongzhang_声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@