file_get_contents:即使出现错误也能获得完整的响应 [英] file_get_contents: get full response even on error

查看:133
本文介绍了file_get_contents:即使出现错误也能获得完整的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使发生错误,是否可以使file_get_contents向我显示实际响应?

Is it possible to make file_get_contents show me the actual response even if an error occurs?

否则很难调试.例如,假设您有以下代码:

It is difficult to debug otherwise. For instance, say you have the following code:

$url = 'https://api.twitter.com/oauth/request_token';
$data = array();

$options = array(
    'http' => array(
    'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
    'method'  => 'POST',
    'content' => http_build_query($data),
    ),
);
$context  = stream_context_create($options);
$result = @file_get_contents($url, false, $context);

var_dump($result);
var_dump($http_response_header);

这向我显示了结果和HTTP标头为NULL,但是我想获取Twitter发回的实际消息(应该是Failed to validate oauth signature and token之类的),如果尝试加载 https://api.twitter.com/oauth/request_token .

This shows me NULL for the result and the HTTP headers, but I want to get the actual message Twitter sends back (should be something like Failed to validate oauth signature and token), which is what you get if you try to load https://api.twitter.com/oauth/request_token in your browser.

推荐答案

有一个非常简单的上下文切换.只需将此行添加到选项:

There is a very simple switch for the context. just add this line to options:

'ignore_errors' => true

所以你会得到

$options = array(
    'http' => array(
    'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
    'method'  => 'POST',
    'content' => http_build_query($data),
    'ignore_errors' => true
    )
);

这篇关于file_get_contents:即使出现错误也能获得完整的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆