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 bo开发者_运维知识库dy of the response?
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);
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
精彩评论