gpt4 book ai didi

php - 运行 Curl 代码时获取 `POST requests require a Content-length header. '

转载 作者:行者123 更新时间:2023-12-04 11:02:30 32 4
gpt4 key购买 nike

我想通过这个 curl 请求获得一个 Auth2 token :

define("CALLBACK_URL", "http://localhost/los/index");
define("AUTH_URL", "https://accounts.google.com/o/oauth2/auth");
define("ACCESS_TOKEN_URL", "https://oauth2.googleapis.com/token");
define("CLIENT_ID", "**.apps.googleusercontent.com");
define("CLIENT_SECRET", "**");
define("SCOPE",
"https://www.googleapis.com/auth/admin.directory.device.chromeos"); // optional


function getToken(){
$curl = curl_init();

$params = array(
CURLOPT_URL => ACCESS_TOKEN_URL."?"
."code=".$code
."&grant_type=authorization_code"
."&client_id=". CLIENT_ID
."&client_secret=". CLIENT_SECRET
."&redirect_uri=". CALLBACK_URL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_NOBODY => false,
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded",
"accept: *",
"accept-encoding: gzip, deflate",
),
);

curl_setopt_array($curl, $params);


$response = curl_exec($curl);
$err = curl_error($curl);
echo $response;
curl_close($curl);

if ($err) {
echo "cURL Error #01: " . $err;
} else {
$response = json_decode($response, true);
if(array_key_exists("access_token", $response)) return $response;
if(array_key_exists("error", $response)) echo $response["error_description"];
echo "cURL Error #02: Something went wrong! Please contact admin.";
}
}

但是,它给我这个错误信息:POST 请求需要 Content-length header 。这就是我们所知道的。

我试图删除 URL 中的换行符,但没有解决问题。 (我跟着this教程)我怎样才能解决这个问题?我是否需要手动放置内容长度 header ?

最佳答案

您需要将 Content-Length header 添加到您的 CURLOPT_HTTPHEADER 数组。

该值通常是正文的大小(以字节为单位)。由于您没有任何值,我建议尝试将 0 作为值。

CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded",
"accept: *",
"accept-encoding: gzip, deflate",
"Content-Length: 0"
)

关于php - 运行 Curl 代码时获取 `POST requests require a Content-length header. ',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58711149/

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