Erik Sapir
Erik Sapir

Reputation: 24747

Why fopen return invalid handle when server returns error

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

Upvotes: 4

Views: 413

Answers (2)

Matty
Matty

Reputation: 34523

Better yet, use the cURL extension instead. Faster performance, finer-grained controls over its behavior, as well as more powerful - you'd be able to retrieve the exact HTTP status code in this cast.

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

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

Upvotes: 0

netcoder
netcoder

Reputation: 67735

Use a context (via stream_context_create) and the ignore_errors context option, that "Fetch the content even on failure status codes.":

$options = array(
    'http' => array(
        'ignore_errors' => true,
    ),
);

$context = stream_context_create($options);

$handle = fopen('http://url/', 'r', false, $context);

Upvotes: 6

Related Questions