繁体   English   中英

如何通过 cURL 正确地向 REST API 发送 POST 请求?

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

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

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

试试这个 class for PHP 作曲家,不要浪费时间

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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