PHP捕获file_get_content错误的正确姿势
file_get_content是可以用来请求超链接的,但是如何捕获错误呢?查了下,示例代码如下:
if (($data = @file_get_contents("https://www.google.com")) === false) {
$error = error_get_last();
echo "HTTP request failed. Error was: " . $error['message'];
} else {
echo "Everything went better than expected";
}
参考资料
How can I handle the warning of file_get_contents() function in PHP?