忽略file_get_contents HTTP包装器中的错误?

sha*_*o86 14 php file-get-contents

下面的代码是查询我正在建立的大学项目的搜索引擎的在线词库,但我遇到了file_get_contents "未能打开流"错误的问题.当我发送词库时,词库无法识别,它会引发错误.我正在尝试编写一段代码来忽略错误,只是在没有信息的情况下继续进行.

$thesaurus_search="http://words.bighugelabs.com/api/2/0089388bb57f/".$this->formatted_query."/php";
$result_thesaurus=file_get_contents($thesaurus_search);
Run Code Online (Sandbox Code Playgroud)

我试过了:

if (file_get_contents($thesaurus_search) != NULL)
{ // do stuff }
Run Code Online (Sandbox Code Playgroud)

...但它不起作用,因为它仍然返回某种字符串.

我该怎么处理这种情况?

net*_*der 34

如果您不希望file_get_contentsHTTP错误报告为PHP警告,那么这是使用流上下文(有一些专门用于此目的)的干净方式:

$context = stream_context_create(array(
    'http' => array('ignore_errors' => true),
));

$result = file_get_contents('http://your/url', false, $context);
Run Code Online (Sandbox Code Playgroud)

  • 感谢 Netcoder 那好多了,感谢您的帮助。我也不会再用扳手敲打我的电脑了 :) (2认同)