Ho una list di proxy, ho bisogno di usarli in uno script php che sto scrivendo.
Come posso verificare che il proxy funzioni prima di usarlo? è ansible? al momento se il proxy non funziona, lo script muore dopo 30 secondi. C'è un modo più rapido per identificare se functionrà o no?
forse creare una connessione socket? submit qualcosa che rivelerà se il proxy è aperto?
Grazie
puoi usare fsockopen
per questo, where puoi specificare un timeout.
Un semplice controllore di liste di proxy. È ansible controllare un elenco ip: port se tale port è aperta su quell'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 ?>
potresti anche voler usare set_time_limit in modo che tu possa eseguire lo script più a lungo.
Codice tratto da: http://www.php.net/manual/en/function.fsockopen.php#95605
Puoi usare questa function
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); }