file_get_contents失败时如何跳过代码?

Lea*_*_51 1 php

当我使用如下所示的有效URL时,file_get_contents将返回html代码

$html = file_get_contents("http://www.google.com/");
Run Code Online (Sandbox Code Playgroud)

但如果网址无效:

$html = file_get_contents("http://www.go1235ogle.com/"); //Invalid URL. This line give Error Message
Run Code Online (Sandbox Code Playgroud)

我的代码将输出一条错误消息 Notice: Trying to get property of non-object

如果file_get_contents无法打开给定的URL,我想隐藏它正在输出的错误消息,并用if else语句跳过下面的代码。

if (FALSE === $html) 
{
  //skip the code in here ?
}
Run Code Online (Sandbox Code Playgroud)

但是上面的代码没有帮助。您能告诉我如何解决此问题吗?

Bar*_*aan 5

您可以通过添加@来隐藏错误

$html = @file_get_contents("http://www.google.com/");
if ($html === FALSE){
  // Error
}
else {
  // No errors
}
Run Code Online (Sandbox Code Playgroud)

资料来源:https : //stackoverflow.com/a/272377/1907875