ドメインにpingを実行し、リクエストの合計サイズとともにレスポンスタイムをリストするPHPスクリプトを作成したいとします。phpを使用してウェブサイトをpingする
これは、ウェブサイトのネットワークを監視するために使用されます。私はcurl
でそれを試してみましたが、ここで私が持っているコードは、これまでです:
function curlTest2($url) {
clearstatcache();
$return = '';
if(substr($url,0,4)!="http") $url = "http://".$url;
$userAgent =
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
$execute = curl_exec($ch);
// Check if any error occured
if(!curl_errno($ch)) {
$bytes = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
$total_time = curl_getinfo($ch, CURLINFO_TOTAL_TIME);
$return = 'Took ' . $total_time . '/Bytes: '. $bytes;
} else {
$return = 'Error reaching domain';
}
curl_close($ch);
return $return;
}
そして、ここでは
function fopenTest($link) {
if(substr($link,0,4)!="http"){
$link = "http://".$link;
}
$timestart = microtime();
$churl = @fopen($link,'r');
$timeend = microtime();
$diff = number_format(((substr($timeend,0,9)) + (substr($timeend,-10)) -
(substr($timestart,0,9)) - (substr($timestart,-10))),4);
$diff = $diff*100;
if (!$churl) {
$message="Offline";
}else{
$message="Online. Time : ".$diff."ms ";
}
fclose($churl);
return $message;
}
fopenを使用して1であることは、PHPを使用してウェブサイトにpingを実行するためのより良い方法はありますか?
drupal.org/handbook/modules/pingについてお聞きしますか?それは私が探しているものに見えません... –
私はまた、これを使用するとうまくいくと思います。賢明な推測では、このオプションは、単純なカールオプションと比べて、内部的に余分なxml解析を必要とするでしょうか?本当におなら。 –