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?
You have to use stream_context_create()
:
$ctx = stream_context_create(array(
'http' => array (
'ignore_errors' => TRUE
)
));
file_get_contents($url, FALSE, $ctx);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With