【问题标题】:Curl and proxies卷曲和代理
【发布时间】:2013-02-07 18:28:19
【问题描述】:

最近写了一个简单的 curl 程序来测试代理。如果我得到 curl 的输出,我使用的代理是好的,我保存它并可以使用它,否则它要么宕机,忙等....

问题是,当我测试一个代理列表时,我得到了输出,但是当我想只使用我从 OK 列表中选择的一个来获取输出时,我什么也得不到,尽管我实际上使用的几乎相同代码。这里是:

测试代码(我正在测试完全相同的代理):

$url='http://google.com';
$curl_imelimit=20;
$allowed_time=19.980;

$file='190.110.86.202:8080
    77.236.209.236:8080
    213.185.231.4:8080
    189.8.32.18:80
    106.51.66.181:80
    190.110.86.22:8080';

preg_match_all('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,4}/', $file, $proxies);

$options=array(
    CURLOPT_REFERER => 'http://google.com',
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_FOLLOWLOCATION => 3,
    CURLOPT_USERAGENT => null,
    CURLOPT_TIMEOUT => $curl_timelimit,
    CURLOPT_URL => $url,
    CURLOPT_PROXY => $proxy);

foreach($proxies as $pr)
    foreach($pr as $proxy){
        $ch=curl_init();
            curl_setopt_array($ch, $options);
            $output=curl_exec($ch);
            $info=curl_getinfo($ch);
            echo $output.'</br>';
        if ($info['total_time']>0 && $info['total_time']<$allowed_time){
            echo '<p style="color:green;">'.$proxy.' OK</p>';
        }
        if ($info['total_time']==0 || $info['total_time']>$allowed_time)
            echo '<p style="color:red;">'.$proxy.' NOT GOOD</p>';
        set_time_limit(30);
        curl_close($ch);             
    }

所有代理都正常工作,及时,我得到了输出。如果我想用上面列表中的一个代理重复它,使用下面的代码,我什么也得不到。 一个代理的代码:

$proxy='190.110.86.202:8080';    
$url='http://google.com';
$curl_timelimit=20;                  

$options=array(
    CURLOPT_REFERER => 'http://google.com',
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_FOLLOWLOCATION => 3,
    CURLOPT_USERAGENT => null,
    CURLOPT_TIMEOUT => $curl_timelimit,
    CURLOPT_URL => $url,
    CURLOPT_PROXY => $proxy);    

$ch=curl_init();
    curl_setopt_array($ch, $options);
    $output=curl_exec($ch);
    $info=curl_getinfo($ch);

curl_close($ch);

    echo $output.'</br>';

【问题讨论】:

    标签: curl proxy


    【解决方案1】:

    您可以使用这个经过良好测试的功能

    function get_page($url)
    
    global $proxy;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    //curl_setopt($ch, CURLOPT_PROXY, $proxy);
    curl_setopt($ch, CURLOPT_HEADER, 0); // return headers 0 no 1 yes
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return page 1:yes
    curl_setopt($ch, CURLOPT_TIMEOUT, 200); // http request timeout 20 seconds
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Follow redirects, need this if the url changes
    curl_setopt($ch, CURLOPT_MAXREDIRS, 2); //if http server gives redirection responce
    curl_setopt($ch, CURLOPT_USERAGENT,
        "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7");
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt"); // cookies storage / here the changes have been made
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // false for https
    curl_setopt($ch, CURLOPT_ENCODING, "gzip"); // the page encoding
    
    $data = curl_exec($ch); // execute the http request
    curl_close($ch); // close the connection
    return $data;
    

    另外,有时端口在您的服务器上被阻止,这就是代理无法正常工作的原因

    【讨论】:

      猜你喜欢
      • 2016-12-10
      • 1970-01-01
      • 2010-12-10
      • 1970-01-01
      • 2012-01-21
      • 2010-12-24
      • 1970-01-01
      • 2010-12-24
      • 2018-05-02
      相关资源
      最近更新 更多