我试图file_get_contents一起使用stream_context_create以发出POST请求。到目前为止,我的代码:
file_get_contents
stream_context_create
$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。有没有办法:
http://php.net/manual/en/reserved.variables.httpresponseheader.php
file_get_contents("http://example.com", false, stream_context_create(['http' => ['ignore_errors' => true]])); var_dump($http_response_header);