PHP 5.3 changes the way file_get_contents works?

I have a strange problem with PHP file_get_contents.

In the past file_get_contents, the remote file returned the text of this file regardless of the HTTP status code returned. If I hit the API and sent back JSON error information with a status of 500, file_get_contentsit gives me that JSON (without indicating that an error code was encountered).

I just installed the Ubuntu 10.04 server, which is the first Ubuntu with PHP 5.3. Instead of giving me JSON, PHP gives a warning when a 500 error is present. As a result, I cannot parse the JSON and give a good error message.

It's nice that PHP notices there an error in the remote file, but I need JSON even (especially!) If there is a 500 error. There seems to be no way to disable this. Has anyone come across this? Any tips?

+3
source share
1 answer

You can tell PHP to ignore stream errors when using file_get_contentsby providing the appropriate stream context (using stream_context_create) using ignore_errorsset to true.

$context = stream_context_create(array('http'=>array('ignore_errors'=>true)));
$contents = file_get_contents($url, FALSE, $context);

You can also look in $http_response_headerfor response headers, including a status code.

+8
source

Source: https://habr.com/ru/post/1744183/


All Articles