In PHP i am using a proxy with curl with the following :
在PHP中,我使用curl的代理,具有以下内容:
CURLOPT_HTTPPROXYTUNNEL, 1,
CURLOPT_PROXY, ''.$current_proxy.'',
and i have a DB with backup proxy ip's in it, but i want to know how i can detect if the proxy has gone down so it can switch the variable $current_proxy
.
我有一个带有备份代理ip的数据库,但我想知道如何检测代理是否已经关闭,以便它可以切换变量$ current_proxy。
What do you suggest for detecting if a server is down? Thanks.
您建议检测服务器是否已关闭?谢谢。
3 个解决方案
#1
3
You can just try to connect to the proxy with fsockopen
like that:
您可以尝试使用fsockopen连接到代理,如下所示:
$proxy = '98.255.255.255:8080';
$timeout = 5;
$splited = explode(':',$proxy); // Separate IP and port
if($con = @fsockopen($splited[0], $splited[1], $errorNumber, $errorMessage, $timeout))
{
echo 'Connection successful, PROXY works!';
} else {
echo $errorNumber . ' ' . $errorMessage;
}
#2
1
list.txt contains such as:
list.txt包含如下:
1.2.3.4:2487 123.123.123.123:3248
etc. php code like this ...
这样的PHP代码...
$url = "http://www.google.com";
$proxies = file("list.txt");
foreach($proxies as $proxy)
{
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_PROXY,$proxy);
curl_setopt($ch,CURLOPT_HTTPPROXYTUNNEL,$proxy);
$page = curl_exec($ch);
curl_close($ch);
$check = stripos($page,'</html>'); // not pretty :)
if($check > 0)
{
echo $proxy . " Works!";
}else{
echo $proxy . " Is Dead!";
}
}
#3
0
Curl doesn't distinguish whether proxy is down or page doesn't exist (or server where this page located is down). That's why you need invent workaround by yourself. At least Daniel from Haxx AB said it.
Curl不区分代理是否已关闭或页面是否不存在(或此页面所在的服务器已关闭)。这就是为什么你需要自己发明解决方法。来自Haxx AB的至少丹尼尔说过。
#1
3
You can just try to connect to the proxy with fsockopen
like that:
您可以尝试使用fsockopen连接到代理,如下所示:
$proxy = '98.255.255.255:8080';
$timeout = 5;
$splited = explode(':',$proxy); // Separate IP and port
if($con = @fsockopen($splited[0], $splited[1], $errorNumber, $errorMessage, $timeout))
{
echo 'Connection successful, PROXY works!';
} else {
echo $errorNumber . ' ' . $errorMessage;
}
#2
1
list.txt contains such as:
list.txt包含如下:
1.2.3.4:2487 123.123.123.123:3248
etc. php code like this ...
这样的PHP代码...
$url = "http://www.google.com";
$proxies = file("list.txt");
foreach($proxies as $proxy)
{
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_PROXY,$proxy);
curl_setopt($ch,CURLOPT_HTTPPROXYTUNNEL,$proxy);
$page = curl_exec($ch);
curl_close($ch);
$check = stripos($page,'</html>'); // not pretty :)
if($check > 0)
{
echo $proxy . " Works!";
}else{
echo $proxy . " Is Dead!";
}
}
#3
0
Curl doesn't distinguish whether proxy is down or page doesn't exist (or server where this page located is down). That's why you need invent workaround by yourself. At least Daniel from Haxx AB said it.
Curl不区分代理是否已关闭或页面是否不存在(或此页面所在的服务器已关闭)。这就是为什么你需要自己发明解决方法。来自Haxx AB的至少丹尼尔说过。