7

是否可以制作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中加载,这就是您得到的。https://api.twitter.com/oauth/request_token

4

1 回答 1

8

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

'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
    )
);
于 2013-08-29T14:23:34.640 回答