简体   繁体   English

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

[英]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.我正在尝试通过交换从 URL 接收到的代码来获取访问令牌,并且我已经配置了重定向 uri 和其他配置。 The following is my callback.php:以下是我的回调.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试试这个 class for PHP 作曲家,不要浪费时间

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

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

相关问题 如何通过php中的curl调用mongodb rest api? - How do I call mongodb rest api through curl in php? 如何使用PHP cURL将请求发送到DocuSign的创建信封来自文档REST端点? - How do I use PHP cURL to POST a request to DocuSign's Create Envelope From Document REST endpoint? 如何通过mongolab rest api创建Web服务,以便在php中使用curl进行基本数据库操作? - how do I create web service through mongolab rest api for basic database operation using curl in php? CURL 发布请求到 API 循环通过数据库 - CURL Post Request to API Looping Through Database 如何发送发布请求到WordPress的REST API有自定义字段? - How to send post request to Wordpress rest api have custom fields? 如何使用 PHP 通过 cURL 向 REST API 发送 HTTP GET 请求,并使用 cURL 从 REST API 调用方法 - How to send HTTP GET request to REST API via cURL using PHP and call method from REST API with cURL 如何使用 PHP 发送 POST 请求? - How do I send a POST request with PHP? 如何使用 fetchAPI 发送发布请求 - how do I send post request with fetchAPI API需要编码的API密钥,我该如何使用curl发送它? - API requires a encoded API key, how do i send this with curl? 如何在 WordPress 中使用 cURL 发送 POST 请求? - How to send POST request with a cURL in WordPress?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM