我怎样才能在PHP中处理file_get_contents()函数的警告?

我写了这样一个PHP代码

$site="http://www.google.com"; $content = file_get_content($site); echo $content; 

但是,当我从$site删除“http://”我得到以下警告:

警告:file_get_contents(www.google.com)[function.file-get-contents]:未能打开stream:

我试图try ,但没有奏效。

第一步:检查返回码: if($content === FALSE) { // handle error here... }

步骤2:通过在调用file_get_contents()之前放置错误控制运算符 (即@ )来禁止警告: $content = @file_get_contents($site);

您也可以将您的error handling程序设置为调用exception的匿名函数 ,并对该exception使用try / catch。

 set_error_handler( create_function( '$severity, $message, $file, $line', 'throw new ErrorException($message, $severity, $severity, $file, $line);' ) ); try { file_get_contents('www.google.com'); } catch (Exception $e) { echo $e->getMessage(); } restore_error_handler(); 

看起来像很多代码来捕捉一个小错误,但如果你在整个应用程序中使用exception,你只需要做一次,在顶部(例如在一个包含的configuration文件中),它会将所有的错误转换为例外。

我最喜欢的做法很简单:

 if (!$data = file_get_contents("http://www.google.com")) { $error = error_get_last(); echo "HTTP request failed. Error was: " . $error['message']; } else { echo "Everything went better than expected"; } 

我在上面用@enobrev的try/catch实验之后发现了这个,但是这样可以减less冗长(和IMO,更可读)的代码。 我们只使用error_get_last来获取最后一个错误的文本,而file_get_contents在失败时返回false,所以一个简单的“if”可以捕捉到这个错误。

你可以添加一个@: $content = @file_get_contents($site);

这将禁止任何警告 – 谨慎使用! 。 请参阅错误控制操作员

编辑:当你删除'http://'你不再寻找一个网页,而是一个名为“www.google …..”的磁盘上的文件

一种替代方法是压制这个错误,并且抛出一个你可以稍后捕获的exception。 如果在代码中有多个对file_get_contents()的调用,这是特别有用的,因为您不需要手动禁止和处理所有的文件。 相反,可以在一个try / catch块中对这个函数进行几次调用。

 // Returns the contents of a file function file_contents($path) { $str = @file_get_contents($path); if ($str === FALSE) { throw new Exception("Cannot access '$path' to read contents."); } else { return $str; } } // Example try { file_contents("a"); file_contents("b"); file_contents("c"); } catch (Exception $e) { // Deal with it. echo "Error: " , $e->getMessage(); } 

以下是我做到的…不需要尝试抓取块…最好的解决scheme总是最简单的…享受!

 $content = @file_get_contents("http://www.google.com"); if (strpos($http_response_header[0], "200")) { echo "SUCCESS"; } else { echo "FAILED"; } 
 function custom_file_get_contents($url) { return file_get_contents( $url, false, stream_context_create( array( 'http' => array( 'ignore_errors' => true ) ) ) ); } $content=FALSE; if($content=custom_file_get_contents($url)) { //play with the result } else { //handle the error } 

以下是我如何处理:

  $this->response_body = @file_get_contents($this->url, false, $context); if ($this->response_body === false) { $error = error_get_last(); $error = explode(': ', $error['message']); $error = trim($error[2]) . PHP_EOL; fprintf(STDERR, 'Error: '. $error); die(); } 

最好的办法是设置你自己的错误和exception处理程序,它会做一些有用的事情,比如将它们logging在文件中或发送给重要的程序。 http://www.php.net/set_error_handler

你可以使用这个脚本

 $url = @file_get_contents("http://www.itreb.info"); if ($url) { // if url is true execute this echo $url; } else { // if not exceute this echo "connection error"; } 

由于PHP 4使用error_reporting() :

 $site="http://www.google.com"; $old_error_reporting = error_reporting(E_ALL ^ E_WARNING); $content = file_get_content($site); error_reporting($old_error_reporting); if ($content === FALSE) { echo "Error getting '$site'"; } else { echo $content; } 

这将尝试获取数据,如果它不起作用,它会捕获错误,并允许你做任何你需要的内容。

 try { $content = file_get_contents($site); } catch(\Exception $e) { return 'The file was not found'; } 
 try { $site="http://www.google.com"; $content = file_get_content($site); echo $content; } catch (ErrorException $e) { // fix the url } set_error_handler(function ($errorNumber, $errorText, $errorFile,$errorLine ) { throw new ErrorException($errorText, 0, $errorNumber, $errorFile, $errorLine); }); 

更改文件php.ini

 allow_url_fopen = On allow_url_include = On 

你也应该设置

allow_url_use = On

在你的php.ini中停止接收警告。

您应该先使用file_exists()函数来使用file_get_contents()。 用这种方法,你将避免PHP的警告。

 $file = "path/to/file"; if(file_exists($file)){ $content = file_get_contents($file); }