2011-02-12 38 views
1

我有一个代理列表,需要在我正在写的一个php脚本中使用它们。帮助测试代理如何使用php

如何在使用代理之前测试代理是否可以正常工作?那可能吗?此时如果代理不工作,脚本会在30秒后死亡。有没有更快的方法来确定它是否会起作用?

也许创建套接字连接?向它发送一些信息,揭示代理是否开放?

感谢

+0

利用它们发送垃圾邮件,对吧? – 2011-02-12 12:07:21

+0

哈哈哈不,哈哈不是垃圾邮件。 – Jason 2011-02-12 12:20:02

回答

6

您可以使用fsockopen这一点,在那里你可以指定一个超时。

一个简单的代理列表检查器。如果该端口在该IP上打开,则可以检查列表ip:port。

<?php 

$fisier = file_get_contents('proxy_list.txt'); // Read the file with the proxy list 
$linii = explode("\n", $fisier); // Get each proxy 
$fisier = fopen("bune.txt", "a"); // Here we will write the good ones 

for($i = 0; $i < count($linii) - 1; $i++) test($linii[$i]); // Test each proxy 

function test($proxy) 
{ 
    global $fisier; 
    $splited = explode(':',$proxy); // Separate IP and port 
    if($con = @fsockopen($splited[0], $splited[1], $eroare, $eroare_str, 3)) 
    { 
    fwrite($fisier, $proxy . "\n"); // Check if we can connect to that IP and port 
    print $proxy . '<br>'; // Show the proxy 
    fclose($con); // Close the socket handle 
    } 
} 

fclose($fisier); // Close the file 

?> 

您可能还需要使用set_time_limit,使您可以更长时间运行的脚本。取自

代码:http://www.php.net/manual/en/function.fsockopen.php#95605

1

您可以使用此功能

function check($proxy=null) 
    { 
     $proxy= explode(':', $proxy); 
     $host = $proxy[0]; 
     $port = $proxy[1]; 
     $waitTimeoutInSeconds = 10; 
     if($fp = @fsockopen($host,$port,$errCode,$errStr,$waitTimeoutInSeconds)){ 
      return 'yes'; 
     } else { 
      return 'false'; 
     } 
     fclose($fp); 
    }