我正在尝试使用file_get_contents
withstream_context_create
来发出 POST 请求。到目前为止我的代码:
$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
并返回假。有没有办法:
- 抑制警告(我打算在失败的情况下抛出我自己的异常)
- 从流中获取错误信息(至少是响应代码)