dongxing2692 2016-08-04 17:36
浏览 45

用随机代理卷曲

I'm going insane trying to figure out what is the problem but I can't find it.

$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);

It should fetch a random proxy IP from the array and then use it in cURL request. All data I'm getting is a blank page. In some cases, I get infinite page load with no results whatsoever. What is causing this and how do I fix this? Thanks.

  • 写回答

1条回答 默认 最新

  • dongren1011 2016-08-05 08:58
    关注

    maybe the problem is with your loadProxies(5) return? cus this code works fine right here right now:

    <?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);
    

    also, $data just returns the return code for curl, not the data, because, by default, curl outputs the response body to stdout. if you set CURLOPT_RETURNTRANSFER , it will return the body. (to redirect it to something else, use CURLOPT_FILE )

    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决