【问题标题】:Fetch the response body on failure using file_get_contents使用 file_get_contents 获取失败时的响应正文
【发布时间】:2013-11-26 12:15:55
【问题描述】:

file_get_contents 的文档说

 On failure, file_get_contents() will return FALSE.

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

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

【问题讨论】:

    标签: php ajax file-get-contents


    【解决方案1】:
    $curl = curl_init('http://example.net');
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER,1);
    $result = curl_exec($curl);
    curl_close($curl);
    

    但这可能无法满足您的需求,因为它需要 curl

    你也可以忽略错误,继续使用file_get_contents

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

    【讨论】:

    • 谢谢,选择了 CURL 本身。虽然,如果我提到 'ignore_errors' 它会返回响应的正文而不是 false ?
    • 是的,只是返回正文
    猜你喜欢
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 2016-05-27
    • 2021-07-20
    相关资源
    最近更新 更多