简体   繁体   中英

How do I send POST request to REST API properly through cURL?

I am trying fetch the access token by exchanging the code received from the URL and I have already configured the redirect uri and other configurations. The following is my callback.php:

$code = $_GET["code"];
$client_id = "";//actual client id
$client_secret = ""//actual client secret;
$redirect_uri = "http%3A%2F%2Flocalhost%2Ftest%2Fwebex_integration_test%2Fcallback.php";
$postRequest = array(
          "client_id" => ""//actual client id,
          "client_secret" => ""//actual client secret,
          "code" => $_GET["code"],
          "redirect_uri" => "http%3A%2F%2Flocalhost%2Ftest%2Fwebex_integration_test%2Fcallback.php",
          "code_verifier" => "abc"
        );
$cURLConnection = curl_init("https://api.webex.com/v1/oauth2/token?grant_type=authorization_code&client_id=$client_id&client_secret=$client_secret&code=$code&redirect_uri=$redirect_uri&code_verifier=abc");
      curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $postRequest);
      curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($cURLConnection, CURLOPT_HTTPHEADER, array(
                                          'Content-Type: application/x-www-form-urlencoded',
                                          'Connection: Keep-Alive'
                                          ));
$apiResponse = curl_exec($cURLConnection);
curl_close($cURLConnection);
$jsonArrayResponse = json_decode($apiResponse);
print_r($jsonArrayResponse); 

The above request results in a blank page. I am expecting the access token.

Try this class for PHP composer and don't waste the time

https://github.com/MoovFun/Xo-Curl

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM