我疯了,想弄清楚问题是什么,但我找不到。
$proxies = loadProxies(5);
function getData($proxylist)
{
$rand_proxy = rand(0,count($proxylist)-1);
$url = 'http://www.stackoverflow.com'; //just for example
$agent = "Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/532.4 (KHTML, like Gecko) Chrome/4.0.233.0 Safari/532.4";
$referer = "http://www.google.com/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_PROXY, $proxylist[$rand_proxy]);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
}
getData($proxies);
应该从数组中获取随机代理IP,然后在cURL请求中使用它。我得到的所有数据都是空白页面。在某些情况下,我得到无限的页面加载,没有任何结果。造成这种情况的原因是什么?如何解决这个问题?感谢。
答案 0 :(得分:0)
也许问题是你的loadProxies(5)返回? cus这段代码现在正常工作:
<?php
$proxies = array('86.188.142.244:8080'); // random public http proxy
function getData($proxylist)
{
$rand_proxy = rand(0,count($proxylist)-1);
$url = 'http://www.stackoverflow.com'; //just for example
$agent = "Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/532.4 (KHTML, like Gecko) Chrome/4.0.233.0 Safari/532.4";
$referer = "http://www.google.com/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_PROXY, $proxylist[$rand_proxy]);
$data = curl_exec($ch);
if($data!==true){$ex=new RuntimeException('curl_exec error. errno: '.curl_errno($ch).' error: '.curl_error($ch));@curl_close($ch);throw $ex;}
curl_close($ch);
//echo $data;
}
getData($proxies);
另外,$ data只返回curl的返回码,而不是数据,因为默认情况下curl会将响应体输出到stdout。如果设置CURLOPT_RETURNTRANSFER,它将返回正文。 (要将其重定向到其他内容,请使用CURLOPT_FILE)