get请求
function curlGet($url, array $header = array(), $timeout = 5)
{
$ch = curl_init();//初始化
curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0);//是否显示头信息,1显示,0不显示
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);//服务器5秒内没有响应,脚本就会断开连接
//url包含https,不验证证书下同
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36');
if(!empty($header)){
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, 0);//返回response头部信息
}
$file_contents = curl_exec($ch);//运行curl
curl_close($ch);
return $file_contents;
}
post请求, 请求参数是表单
function curlPost($url, array $params = array(), array $header = array(), $timeout = 5)
{
$ch = curl_init();//初始化
curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0);//是否显示头信息,1显示,0不显示
//curl_setopt($ch,CURLOPT_REFERER,'https://lbs.amap.com/api/webservice/guide/api/weatherinfo/');//伪造referer
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
// 拼接请求参数
$params2 = '';
foreach ( $params as $k => $v )
{
$params2 .= "$k=" . urlencode($v). "&" ;
}
$params2 = substr($params2,0,-1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params2);
//url包含https,不验证证书下同
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36');
if(!empty($header)){
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, 0);//返回response头部信息
}
$data = curl_exec($ch);//运行curl
if($data === false){
echo '<br>出现错误:'.curl_error($ch);
}
curl_close($ch);
return ($data);
}
post请求, 参数是json
function curlPostJson($url, $params = '', array $header = array(), $timeout = 5)
{
$ch = curl_init();//初始化
curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0);//是否显示头信息,1显示,0不显示
//curl_setopt($ch,CURLOPT_REFERER,'https://lbs.amap.com/api/webservice/guide/api/weatherinfo/');//伪造referer
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
//url包含https,不验证证书下同
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36');
if(!empty($header)){
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, 0);//返回response头部信息
}
$data = curl_exec($ch);//运行curl
if($data === false){
echo '<br>出现错误:'.curl_error($ch);
}
curl_close($ch);
return ($data);
}
curl遇到重定向的解决方法
function curlGet($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, true);
//函数中加入下面这条语句
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
return curl_exec($ch);
}
标签:ch,封装,setopt,url,header,curl,php,CURLOPT
From: https://blog.51cto.com/ding/8425466