Tyl*_*ter 41
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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是从抓住这个职位上如何检查是否存在URL.因为Twitter应该在维护时提供300以上的错误消息,或者404,这应该是完美的.
kar*_*m79 21
这是一个:
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=8&txtCodeId=1786
另一个:
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);
Run Code Online (Sandbox Code Playgroud)
使用shell_exec:
<?php
$output = shell_exec('ping -c1 google.com');
echo "<pre>$output</pre>";
?>
Run Code Online (Sandbox Code Playgroud)
另一个选项(如果您需要/想要ping而不是发送HTTP请求)是PHP的Ping类.我为此目的编写了它,它允许您使用三种支持的方法之一来ping服务器(某些服务器/环境仅支持三种方法中的一种).
用法示例:
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.';
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
118724 次 |
最近记录: |