PHP 8.6.0 Beta 1 is available for testing

Voting

: max(three, five)?
(Example: nine)

The Note You're Voting On

vanessa at reteguin dot com
27 days ago
When coming across a non-2xx status code as a response (of an API call for example), file_get_contents returns directly false and shows a warning, even if the given response still holds data (is not empty). In other words, file_get_contents reports *HTTP errors* as PHP Warnings and therefore it stops running.

To retrieve said response body without falling for a false, as pointed out by netcoder (https://stackoverflow.com/a/11479968), create a custom HTTP context configuration with ignore_errors set to true:

```php
$context = stream_context_create(
        'http' => [
        'ignore_errors' => true,
    ]
);

$body = file_get_contents('http://example.com/section', false, $context);
echo $body
```

<< Back to user notes page

To Top