如果使用 file_get_contents() 连接到 Facebook,
$response = file_get_contents("https://graph.facebook.com/...?access_token=***");
echo "Response: ($response)\n";
并且服务器返回一个非 OK 的 HTTP 状态,PHP 给出一个一般的错误响应,并抑制响应。返回的主体是空的。
file_get_contents(...): failed to open stream: HTTP/1.0 400 Bad Request
Response: ()
但是如果我们使用 cURL,我们会看到 Facebook 实际上返回了一个有用的响应正文:
{"error":{"message":"An active access...","type":"OAuthException","code":2500}}
如何使 file_get_contents() 返回响应正文而不考虑 HTTP 错误?
最佳答案
你必须使用stream_context_create()
:
$ctx = stream_context_create(array(
'http' => array (
'ignore_errors' => TRUE
)
));
file_get_contents($url, FALSE, $ctx);
关于php - 尽管 HTTP 错误,使 file_get_contents() 返回服务器响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18376015/