使用file_get_contents获取失败时的响应正文


Fetch the response body on failure using file_get_contents

file_get_contents的文档显示

 On failure, file_get_contents() will return FALSE.

我正在与一个系统集成,该系统在响应中返回错误消息,并将状态代码设置为"50x"

有没有办法,我仍然可以获取响应内容?

$curl = curl_init('http://example.net');
curl_setopt( $curl, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($curl);
curl_close($curl);

然而,这可能无法满足您的需求,因为它需要卷曲

您也可以忽略错误,仍然使用file_get_contents

$contents = file_get_contents($url, FALSE, stream_context_create(array(
    'http' => array(
        'ignore_errors' => true
     )
));