52
我試圖使用file_get_contents
和stream_context_create
一起發出POST請求。到目前爲止我的代碼:帶有file_get_contents的HTTP請求,獲取響應代碼
$options = array('http' => array(
'method' => 'POST',
'content' => $data,
'header' =>
"Content-Type: text/plain\r\n" .
"Content-Length: " . strlen($data) . "\r\n"
));
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
它工作正常,然而,當HTTP錯誤發生,它吐出了一個警告:
file_get_contents(...): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request
並返回false。有沒有一種辦法:
- 抑制警告(我打算把我自己的異常失敗的情況下),從流
- 獲得錯誤信息(至少,響應代碼)
我知道如何做到這一點與捲曲,我的問題是具體關於流/文件功能。
對不起,如果這是rtm,流對我來說是新東西。
對於那些想知道,在回答第一個問題是' 'ignore_errors'=> TRUE'添加到'$ options'。 – georg 2013-03-25 16:55:50
@georg你也可以在行的開頭加上'@'。 – FluorescentGreen5 2017-02-16 22:34:05