Why fopen returns an invalid handle when the server returns an error

When the server returns any error (401, 405, etc.), fopen return invalid. Is there any way to get the body of the response?

+4
source share
2 answers

Use the context (via stream_context_create ) and the context ignore_errors option that "Get content even by failure status codes" .:

$options = array( 'http' => array( 'ignore_errors' => true, ), ); $context = stream_context_create($options); $handle = fopen('http://url/', 'r', false, $context); 
+6
source

It is better to use the cURL extension. Faster performance, finer control over its behavior, as well as more powerful - you can get the exact HTTP status code in this application.

Here is a good example: http://php.net/curl.examples-basic

Here's the full documentation: http://php.net/book.curl

+1
source

All Articles