2013-02-07 143 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; 

還,有時端口被阻擋在服務器這就是爲什麼該代理不工作