帮助测试代理是否使用 php 工作 [英] Help to test if a proxy works using php

查看:52
本文介绍了帮助测试代理是否使用 php 工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a list of proxies, need to use them in a php script I am writing.

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

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?

谢谢

推荐答案

您可以使用 fsockopen 来解决这个问题,您可以在其中指定超时时间.

you can use fsockopen for this, where you can specify a timeout.

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

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

?>

您可能还想使用 set_time_limit 以便您可以运行您的脚本更长.

you may also want to use set_time_limit so that you can run your script for longer.

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

这篇关于帮助测试代理是否使用 php 工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆