[英]file_get_contents() handling error message
我有一個工作功能來獲得外部網站的速度。
$t = microtime( TRUE );
file_get_contents( "http://www.example.org" );
$t = microtime( TRUE ) - $t;
print "It took $t seconds!";
但是,如果外部 url 關閉,則會打印此內容;
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!
打印“站點關閉”而不是收到該錯誤的正確方法是什么?
您需要檢查對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!";
}
@
是用來抑制警告的。 還要注意===
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.