帮助测试代理是否使用PHP工作

Jas*_*son 1 php testing proxy

我有一个代理列表,需要在我写的PHP脚本中使用它们.

在使用代理之前,如何测试代理是否可行?那可能吗?目前,如果代理不起作用,脚本会在30秒后死亡.有没有更快捷的方法来确定它是否有效?

也许创建套接字连接?发送一些东西,揭示代理是否开放?

谢谢

Rob*_*itt 9

你可以使用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

?>
Run Code Online (Sandbox Code Playgroud)

您可能还想使用set_time_limit,以便可以更长时间地运行脚本.

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