douye2020 2014-08-07 04:24
浏览 115

捕获JSON错误?

i am searching for a simple way to catch JSON-Errors and put out my own Errormessage. To give you an example, i have code that looks like this:

$response = json_decode(file_get_contents($url));

This works totally fine, but after while the URL does not contain more content. I know that, and it is totally fine, but i do not want that an error gets thrown all the time that looks like this:

Warning: file_get_contents(http://example.com/getcontent.php): failed to open stream: HTTP request failed! HTTP/1.1 400 BAD REQUEST in C:\Program Files... on line 153

The first thing i tried was to make a try/catch-statement which looked like this:

try
{
    $response = json_decode(file_get_contents($url));
}
catch (Exception $e)
{
    echo "Error!";
}

This did not change anything.

Thank you very much in advance!

  • 写回答

1条回答 默认 最新

  • dsfdfd1211 2014-08-07 04:45
    关注

    First of all, thank you very much for your fast responses to my question. I did realize that i was to quick in posting that question and i did not read carefully enough. I thought the error was produced by JSON_DECODE but in fact it was FILE_GET_CONTENTS! So when i searched for this i already found a perfect answer here on this site:

    How can I handle the warning of file_get_contents() function in PHP?

    Thank you all very much again.

    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图