为什么fopen在服务器返回错误时返回无效句柄

Eri*_*pir 4 php http

当服务器返回任何错误(401,405等)时,fopen返回无效.有没有办法接收回复的正文?

net*_*der 6

使用上下文(通过stream_context_create)和ignore_errors上下文选项,即" 即使在失败状态代码上获取内容. ":

$options = array(
    'http' => array(
        'ignore_errors' => true,
    ),
);

$context = stream_context_create($options);

$handle = fopen('http://url/', 'r', false, $context);
Run Code Online (Sandbox Code Playgroud)