curl模拟浏览器,ip,来源,进行网站采集的实现方法
function doCurl($url, $data=array(), $header=array(), $referer='', $timeout=30){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);//设置是否在返回结果中显示header信息
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//设置请求 header信息 包括
curl_setopt($ch, CURLOPT_POST, true);// 是否post提交数据
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));//post参数
curl_setopt($ch, CURLOPT_REFERER, $referer); //模拟来源
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//返回结果 true 保持到字符串中 false 直接输出
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); //请求超时
$response = curl_exec($ch); //执行curl请求
if($error=curl_error($ch)){
die($error);
}
curl_close($ch); //关闭请求
return $response;
}
// demo1
$url = 'http://www.example.cn/server.php';
$response = doCurl($url);
echo $response;
// demo2
$url = 'http://www.example.cn/server.php';//访问url地址
//post提交数据
$data = array();
// 设置IP 设置浏览器标识
$header = array(
'CLIENT-IP: 192.168.0.100',
'X-FORWARDED-FOR: 192.168.0.100',
'User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.33 Safari/537.36',
);
// 设置来源
$referer = 'http://www.xxx.cn/';
// 调用curl函数方法
$response = doCurl($url, $data, $header, $referer, 30);
//输出结果
echo $response;