gpt4 book ai didi

php - 文件获取内容 : get full response even on error

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:35:44 25 4
gpt4 key购买 nike

是否可以让 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 header 显示为 NULL,但我想获取 Twitter 发回的实际消息(应该类似于 Failed to validate oauth signature and token),这就是如果你尝试加载 https://api.twitter.com/oauth/request_token 你会得到在您的浏览器中。

最佳答案

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

'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
)
);

关于php - 文件获取内容 : get full response even on error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18513462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com