Ping 站点并在 PHP 中返回结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1239068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ping site and return result in PHP
提问by Tomer Lichtash
I'd like to create a small IF procedure that will check if Twitter is available (unlike now, for example), and will return true or false.
我想创建一个小的 IF 程序来检查 Twitter 是否可用(例如,与现在不同),并返回 true 或 false。
Help :)
帮助 :)
回答by Tyler Carter
function urlExists($url=NULL)
{
if($url == NULL) return false;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode>=200 && $httpcode<300){
return true;
} else {
return false;
}
}
This was grabbed from this poston how to check if a URL exists. Because Twitter should provide an error message above 300 when it is in maintenance, or a 404, this should work perfectly.
这是从这篇关于如何检查 URL 是否存在的帖子中获取的。因为 Twitter 在维护时应该提供 300 以上的错误消息,或者 404,所以这应该可以正常工作。
回答by karim79
Here's one:
这是一个:
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=8&txtCodeId=1786
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=8&txtCodeId=1786
Another:
其他:
function ping($host, $port, $timeout) {
$tB = microtime(true);
$fP = fSockOpen($host, $port, $errno, $errstr, $timeout);
if (!$fP) { return "down"; }
$tA = microtime(true);
return round((($tA - $tB) * 1000), 0)." ms";
}
//Echoing it will display the ping if the host is up, if not it'll say "down".
echo ping("www.google.com", 80, 10);
回答by Elzo Valugi
Using shell_exec:
使用shell_exec:
<?php
$output = shell_exec('ping -c1 google.com');
echo "<pre>$output</pre>";
?>
回答by geerlingguy
Another option (if you need/want to ping instead of send an HTTP request) is the Ping class for PHP. I wrote it for just this purpose, and it lets you use one of three supported methods to ping a server (some servers/environments only support one of the three methods).
另一个选项(如果您需要/想要 ping 而不是发送 HTTP 请求)是PHP的Ping 类。我只是为了这个目的而编写它,它允许您使用三种受支持的方法之一来 ping 服务器(某些服务器/环境仅支持三种方法中的一种)。
Example usage:
用法示例:
require_once('Ping/Ping.php');
$host = 'www.example.com';
$ping = new Ping($host);
$latency = $ping->ping();
if ($latency) {
print 'Latency is ' . $latency . ' ms';
}
else {
print 'Host could not be reached.';
}
回答by Philippe Gerber
pingis available on almost every OS. So you could make a system call and fetch the result.
ping几乎在每个操作系统上都可用。所以你可以进行系统调用并获取结果。
回答by icc97
With the following function you are just sending the pure ICMP packets using socket_create. I got the following code from a user notethere. N.B. You must run the following as root.
使用以下函数,您只需使用socket_create发送纯 ICMP 数据包。我从那里的用户注释中获得了以下代码。注意您必须以root身份运行以下命令。
Although you can't put this in a standard web page you can run it as a cron job and populate a database with the results.
尽管您不能将其放在标准网页中,但您可以将其作为 cron 作业运行并用结果填充数据库。
So it's best suited if you need to monitor a site.
因此,它最适合您需要监控站点的情况。
function twitterIsUp() {
return ping('twitter.com');
}
function ping ($host, $timeout = 1) {
/* ICMP ping packet with a pre-calculated checksum */
$package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";
$socket = socket_create(AF_INET, SOCK_RAW, 1);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0));
socket_connect($socket, $host, null);
$ts = microtime(true);
socket_send($socket, $package, strLen($package), 0);
if (socket_read($socket, 255)) {
$result = microtime(true) - $ts;
} else {
$result = false;
}
socket_close($socket);
return $result;
}
回答by Fathur Rohim
this is php code I used, reply is usually like this:
这是我使用的php代码,回复通常是这样的:
2 packets transmitted, 2 received, 0% packet loss, time 1089ms
So I used code like this:
所以我使用了这样的代码:
$ping_how_many = 2;
$ping_result = shell_exec('ping -c '.$ping_how_many.' bing.com');
if( !preg_match('/'.$ping_how_many.' received/',$ping_result) ){
echo 'Bad ping result'. PHP_EOL;
// goto next1;
}

