php curl 代理 切换 ip


有一个需求,采集网站上的数据,用到了PHP的curl函数来模拟提交参数并采集。
发现对方的服务器有IP限制,同一个IP地址,采集若干条记录以后,就被列为黑名单了,返回404。
用VPN工具换IP后可以正常采集,想用curl函数自带的代理功能实现换IP,可行么?

   
  curl_setopt($ch, CURLOPT_PROXY,             "YOUR PROXY HOST");
  
curl_setopt($ch, CURLOPT_PROXYPORT, "YOUR PROXY PORT");

开启这个后,模拟测试发现服务器端的$_SERVER['REMOTE_ADDR']没有改变过来,还是本地的真实IP。
PHP有没有方法能伪装$_SERVER['REMOTE_ADDR']呢?
实在不行,有没有其它方法能实现这个需求,类似能自动更换IP地址的软件有推荐的么?谢谢

补充:找到一个stackoverflow的回答,没太看明白:
http://stackoverflow.com/questions/5092563/how-to-fake-serverremote-addr-variable



相关链接

php http协议

himiko 10 years, 2 months ago
   
  <?php
  
function curl() {
$headers['CLIENT-IP'] = '202.103.24.40';
$headers['X-FORWARDED-FOR'] = '202.103.24.40';
$headerArr = array();
foreach( $headers as $n => $v ) {
$headerArr[] = $n .':' . $v;
}
$url ="http://model.zhangzhuan.org/toupiao/vote.php?id=63";
$params = array (
'qq' => '821200318' ,
);

$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)' );
curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, ($params) );
curl_setopt ( $ch, CURLOPT_SSLVERSION, 3 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, TRUE ); //
//curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_REFERER, "http://phpspeak.com/ ");
curl_setopt ($ch, CURLOPT_HTTPHEADER , $headerArr ); //构造IP
//curl_setopt( $ch, CURLOPT_HEADER, 1);//此处如果去掉不会再页面显示一些头部信息
$result = curl_exec ( $ch );
$error = curl_error ( $ch );
curl_close ( $ch );
return $result;
}
$a = curl ();
echo $a;

试试这个

冬馬かずさ answered 10 years, 2 months ago

Your Answer