gpt4 book ai didi

php - 返回状态为400时使用phps file_get_contents获取服务器发送的内容

转载 作者:行者123 更新时间:2023-12-04 08:37:51 27 4
gpt4 key购买 nike

我正在使用 phps file_get_contents与我的 API 服务器连接。当服务器返回 200 状态(它从 api 服务器返回内容)时,它运行良好。问题是当 api 服务器返回 400 状态时。如果 api 服务器以 400 状态返回,则该函数仅返回 FALSE(不是内容)。
这是我从 得到的信息$http_response_header

[ "HTTP\/1.1 400 Bad Request", 
"Server: nginx\/1.16.1",
"Date: Fri, 06 Nov 2020 05:39:09 GMT",
"Content-Type: application\/json",
"Content-Length: 117",
"Connection: close",
"Strict-Transport-Security: max-age=31536000; includeSubDomains; preload",
"Access-Control-Allow-Origin: *",
"Access-Control-Expose-Headers: Content-Length,Content-Type,Date,Server,Connection" ]

现在我的问题是如何获得 内容 当状态为 400 时从 API 服务器发送?

最佳答案

您只需要启用 ignore_errors旗帜:

$context = stream_context_create([
'http' => [
'ignore_errors' => true,
],
]);
var_dump(file_get_contents('http://example.com/', false, $context));

关于php - 返回状态为400时使用phps file_get_contents获取服务器发送的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64709660/

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