首页 > 解决方案 > 如何通过 cURL 正确地将 POST 请求发送到 REST API?

问题描述

我正在尝试通过交换从 URL 接收到的代码来获取访问令牌,并且我已经配置了重定向 uri 和其他配置。以下是我的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); 

上述请求导致空白页。我期待访问令牌。

标签: phprest

解决方案


试试这个类的 PHP 作曲家,不要浪费时间

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


推荐阅读