编程语言
首页 > 编程语言> > php-使用file_get_contents获取失败时的响应正文

php-使用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
     )
));

标签:file-get-contents,ajax,php
来源: https://codeday.me/bug/20191122/2059810.html