我有一个工作功能来获取外部网站的速度。
$t = microtime( TRUE );
file_get_contents( "http://www.example.org" );
$t = microtime( TRUE ) - $t;
print "It took $t seconds!";
但是,如果外部网址关闭,它将打印此内容。
Warning: file_get_contents(http://www.example.org): failed to open stream: HTTP request failed!
HTTP/1.0 500 Internal Server Error in /home/china/public_html/dene.php on line 47
It took 14.4556088448 seconds!
什么是corerct方法,打印“ Site down”而不是显示该错误?
您需要检查对的调用是否file_get_contents
成功:
$t = microtime( TRUE );
@$content = file_get_contents( "http://www.example.org" );
if($content === FALSE) {
print "Site down"; // or other problem
} else {
$t = microtime( TRUE ) - $t;
print "It took $t seconds!";
}
在@
那里可以抑制警告。另请注意===
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句