我试图使用file_get_contents和stream_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
并返回false。有办法:
>压制一个警告(我计划在发生故障时抛出我自己的异常)
>从流中获取错误信息(至少,响应代码)
我知道如何做curl,我的问题是专门关于流/文件功能。
对不起,如果这是rtfm,streams的东西是我的新。