4

If using file_get_contents() to connect to Facebook,

$response = file_get_contents("https://graph.facebook.com/...?access_token=***");
echo "Response: ($response)\n";

And the server returns a non-OK HTTP status, PHP gives a generic error response, and suppresses the response. The body returned is empty.

file_get_contents(...): failed to open stream: HTTP/1.0 400 Bad Request
Response: ()

But if we use cURL, we see that Facebook actually returns a useful response body:

{"error":{"message":"An active access...","type":"OAuthException","code":2500}}

How can I make file_get_contents() return the response body regardless of HTTP errors?

4

2 回答 2

9

你必须使用stream_context_create()

$ctx = stream_context_create(array(
    'http' => array (
        'ignore_errors' => TRUE
     )
));


file_get_contents($url, FALSE, $ctx);
于 2013-08-22T09:18:33.713 回答
1

您可以忽略 file_get_contents 抛出的错误

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

$context = stream_context_create($opts);
$file = file_get_contents('https://graph.facebook.com/...?access_token=***', false, $context);

var_dump($file);
于 2013-08-22T09:18:50.353 回答