CURL模拟IP、请求来源
很久以前的代码,当时还在做垃圾站采集内容,但是经常碰到IP被封的情况,所以就做了这个模拟IP和请求来源的东东出来
/**
* 使用自定义IP请求指定的URL
* @param string $url 要请求的URL
* @param string $ip 要模拟的IP
* @return string
*/
function curl_get_contents($url, $ip)
{
$ch = curl_init();
$isSSL = substr($url, 0, 8) == 'https://' ? true : false;
$headerPara = array(
"Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Language:zh-CN,zh;q=0.9",
'Referer: https://www.baidu.com/s?wd=%E7%99%BE%E5%BA%A6&rsv_spt=1&rsv_iqid=0xe5a39f3b0003c303&issp=1&f=8&rsv_bp=0&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_sug3=6&rsv_sug1=4&rsv_sug7=100', //模拟来源为百度搜索
'CLIENT-IP: ' . $ip,
'X-FORWARDED-FOR: ' . $ip
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerPara);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)'); //模拟为百度蜘蛛
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($isSSL) {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
}
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
/**
* 生成随机IP
*/
function randip(){
return rand(13, 255) . '.' . rand(13, 255) . '.' . rand(13, 255) . '.' . rand(13, 255);
}