2013-02-07 74 views
0

最近写了一个简单的卷曲程序来测试代理。如果我得到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>'; 

回答

0

您可以使用此经过充分测试的功能

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; 

还,有时端口被阻挡在服务器这就是为什么该代理不工作