php无法像python的scarpy库一样,绑定网卡。只有每次发送请求时都指定ip。
function getHtml($url)
{
$ip = $this->ip;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://weixin.sogou.com/weixin');
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_MAXREDIRS,1);
curl_setopt($ch, CURLOPT_HTTPHEADER,array("REMOTE_ADDR: $ip", "HTTP_X_FORWARDED_FOR: $ip"));//这一段为指定ip
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$content=curl_exec($ch);
$code=curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_close($ch);
if ($code=='404'){
return false;
}
return $content;
}