php file_get_contents 处理错误的好方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16130818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:34:16  来源:igfitidea点击:

file_get_contents good way to handle errors

phphtml

提问by Hashey100

I am trying to error handle the file_get_contents method so even if the user enters an incorrect website it will echo an error message rather then the unprofessional

我正在尝试错误处理 file_get_contents 方法,因此即使用户输入了错误的网站,它也会回显错误消息而不是不专业的

Warning: file_get_contents(sidiowdiowjdiso): failed to open stream: No such file or directory in C:\xampp\htdocs\test.php on line 6

警告:file_get_contents(sidiowdiowjdiso):无法打开流:第 6 行 C:\xampp\htdocs\test.php 中没有这样的文件或目录

I thought if i make a try and catch it will be able to catch the error but that did not work.

我想如果我尝试并捕获它,它将能够捕获错误,但这不起作用。

try  
{  
$json = file_get_contents("sidiowdiowjdiso", true); //getting the file content
}  
catch (Exception $e)  
{  
 throw new Exception( 'Something really gone wrong', 0, $e);  
}  

回答by Andrey Volk

Try cURL with curl_errorinstead of file_get_contents:

尝试使用curl_error而不是 file_get_contents 的cURL :

<?php
// Create a curl handle to a non-existing location
$ch = curl_init('http://404.php.net/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = '';
if( ($json = curl_exec($ch) ) === false)
{
    echo 'Curl error: ' . curl_error($ch);
}
else
{
    echo 'Operation completed without any errors';
}

// Close handle
curl_close($ch);
?>

回答by m4t1t0

file_get_contentsdo not throw an exception in error, instead it returns false, so you can check if the returned value is false:

file_get_contents不要在错误中抛出异常,而是返回 false,因此您可以检查返回值是否为 false:

$json = file_get_contents("sidiowdiowjdiso", true);
if ($json === false) {
    //There is an error opening the file
}

This way you still get the warning, if you want to remove it, you need to put an @in front of file_get_contents. (This is considered a bad practice)

这样你仍然会收到警告,如果你想删除它,你需要@file_get_contents. (这被认为是一种不好的做法)

$json = @file_get_contents("sidiowdiowjdiso", true);

回答by chrisd84

You could do any of the following:

您可以执行以下任一操作:

Set a global error handler (that will handle WARNINGs as well), for all of your unhandled exceptions: http://php.net/manual/en/function.set-error-handler.php

为所有未处理的异常设置全局错误处理程序(也将处理警告):http://php.net/manual/en/function.set-error-handler.php

Or by checking the return value of the file_get_contents function (with the === operator, as it will return boolean false on failure), and then manage the error message accordingly, and disable the error reporting on the function by prepending a "@" like so:

或者通过检查 file_get_contents 函数的返回值(使用 === 运算符,因为它会在失败时返回布尔值 false),然后相应地管理错误消息,并通过添加“@”来禁用函数的错误报告像这样:

$json = @file_get_contents("file", true);
if($json === false) {
// error handling
} else {
// do something with $json
}

回答by Rubin Porwal

As a solution to your problem please try executing following code snippet

作为您问题的解决方案,请尝试执行以下代码片段

 try  
{  
  $json = @file_get_contents("sidiowdiowjdiso", true); //getting the file content
  if($json==false)
  {
     throw new Exception( 'Something really gone wrong');  
  }
}  
catch (Exception $e)  
{  
  echo $e->getMessage();  
}