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