file_get_contents:即使出错也会得到完整的响应

时间:2013-08-29 14:17:24

标签: php error-handling file-get-contents

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

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

$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),如果你尝试加载{{{ 3}}在您的浏览器中。

1 个答案:

答案 0 :(得分:5)

上下文有一个非常简单的开关。只需将此行添加到选项:

'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
    )
);
相关问题