Getting empty response in Get Token

Hi,

I am using following PHP code to Get Token after getting redirected by the authorization step.

<?php
$cd = $_GET['code'];
$url = 'https://api.upstox.com/v2/login/authorization/token';
$headers = [
    'accept: application/json',
    'Content-Type: application/x-www-form-urlencoded',
];
$data = [
    'code' => $cd,
    'client_id' => 'xxx', /* same as used earlier */
    'client_secret' => 'yyy', /* same as used in authorize step */
    'redirect_uri' => 'ddd', /* this is the url that runs this script. same as authorize step*/
    'grant_type' => 'authorization_code',
];

$jsonString = json_encode($data);
echo $jsonString;
$ch = curl_init($url); // shows that the 'code' is getting passed correctly

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo "Response Code: $httpCode\n";
echo "Response Data: $response\n";
?>

The ā€œcodeā€ received as part of GET parameter is getting passed correctly as printed by the echo string. However after executing the Curl URL, it is posting empty response as in
ā€œResponse Code:0 Response Data:ā€

The Client Id, Secret and redirect URL are same as that were passed to the authorize URL..

The above code is copied from Developer API documentation page from Upstox → Get Token | Upstox Developer API

Also, earlier I was using DOT NET application to get the access token using Selenium Crome drivers and that is still working all good. However I want to shift to PHP now.

Please help.

Regards,

Alok

The problem solved now. It was some cURL SSL certificate validation error. Added following lines to the code and it solved the issue.

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

Regards,

Alok.

2 Likes