Help to test if a proxy works using php
you can use fsockopen
for this, where you can specify a timeout.
A simple proxy list checker. You can check a list ip:port if that port is opened on that IP.
<?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
?>
you may also want to use set_time_limit so that you can run your script for longer.
Code taken from: http://www.php.net/manual/en/function.fsockopen.php#95605
Jason
I'm a web developer who works in PHP/MySQL/SQLite and Server side Javascript: http://bondijs.org
Updated on June 04, 2022Comments
-
Jason almost 2 years
I have a list of proxies, need to use them in a php script I am writing.
How can I test that the proxy will work before I use it? is that possible? at the moment if the proxy doesn't work the script just dies after 30 seconds. Is there a quicker way to identify whether it will work or not?
perhaps create a socket connection? send something to it that will reveal whether the proxy is open?
thanks
-
khoekman almost 7 yearsyou should return a boolean value true or false. The string 'false' will be evaluated to boolean true. This will lead to bugs. And the fclose will never be reached because you always return in the if/else.
-
Salem about 6 yearsBTW you can reduce fsockopen time out as low as
0.1
second I test it and reduced 16 proxy list time from3.7
sec to1.4
,Edit
I even go further to0.01
total time were84
sec -
DJQ over 2 yearsPlus you return something before closing the socket. The shortened version would be: ` function testProxy(string $host, int $port) :bool { $r = false; $waitTimeoutInSeconds = 4; if ($fp = @fsockopen($host, $port, $errCode, $errStr, $waitTimeoutInSeconds)) $r = true; fclose($fp); return $r; }`
-
Ruydo over 2 yearsits ok now :) .