我刚遇到类似问题并解决了它。我的解决方案涵盖更多的案例:
问:如何在PHP中使用POST,而不使用cURL?
答:使用file_get_contents()。
问:如何让file_get_contents()不会抱怨错误HTTP状态?
答:在传递给file_get_contents()的选项中设置ingore_errors => TRUE。
问:如何在响应中检索http状态?
答:经过file_get_contents()函数调用,评估$ http_response_header
问:如何检索响应体甚至错误HTTP响应代码?
答:设置ignore_errors => TRUE,file_get_contents()将返回正文。
下面是代码示例:
$reqBody = '{"description":"test"}';
$opts = array('http' =>
array(
'method' => 'POST',
'header' => "Content-Type: application/json\r\n",
'ignore_errors' => TRUE,
'content' => $reqBody
)
);
$context = stream_context_create($opts);
$url = 'http://mysite';
// with ignore_errors=true, file_get_contents() won't complain
$respBody = file_get_contents($url, false, $context);
// evaluate the response header, the way you want.
// In particular it contains http status code for response
var_dump($http_response_header);
// with ignore_errors=true, you get respBody even for bad http response code
echo $respBody;
我相信你有一个错字在required_functionü忘记修正正 –
,这是我从你们所需要的功能:)如果可能的话 –
哦,好的,我个人从来没有处理一个403 ...但我知道你可以处理404从.htaccess它重定向到一个自定义页面...也许尝试吗? –