使用cURL连接到OpenId会产生错误

I am using the following code to access an OpenId system and request a bearer token.

if (isset($_GET['code'])) {
    // try to get an access token
    $code = $_GET['code'];
    $url = 'https://3rdpartydomain.com/connect/token';
    $params = array(
        "code" => $code,
        "client_id" => CLIENTID,
        "client_secret" => CLIENTSECRET,
        "redirect_uri" => 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"],
        "grant_type" => "authorization_code"
    );

    $ch = curl_init();
    curl_setopt($ch, constant("CURLOPT_" . 'URL'), $url);
    curl_setopt($ch, constant("CURLOPT_" . 'POST'), true);
    curl_setopt($ch, constant("CURLOPT_" . 'POSTFIELDS'), $params);
    $output = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);
    if ($info['http_code'] === 200) {
        header('Content-Type: ' . $info['content_type']);
        return $output;
    } else {
        return 'An error happened';
    }
} else {

    $url = "https://3rdpartydomain.com/connect/token";

    $params = array(
        "response_type" => "code",
        "client_id" => CLIENTID,
        "redirect_uri" => 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"],
        "scope" => "openid"
    );

    $request_to = $url . '?' . http_build_query($params);

    header("Location: " . $request_to);
}

However, I'm getting the following message:

{"Message":"The requested resource does not support http method 'GET'."}

Can someone help identify the correct to my code please?

Thanks!

Add these:

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

There is probably a change of requested method in the redirect from your IdP. I've seen this issue when I was testing my OIDC with curl -XPOST. The workaround was to remove that request type specification. Try to comment your line: curl_setopt($ch, constant("CURLOPT_" . 'POST'), true);