Reputation: 458
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!
Upvotes: 0
Views: 71
Reputation: 458
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.
Upvotes: 1