0
votes

I'm trying to request an access token using a PHP script. I'm using curl to post my API key and secret and expecting to receive a JSON response which includes an access token. I'm failing (I don't get a response). Am I missing something in my code? I would have prefered to use an SDK but Amadeus does not have one for php.

$url = ' https://test.api.amadeus.com/v1/security/oauth2/token';
$curls = curl_init();
curl_setopt($curls, CURLOPT_URL, $url);
curl_setopt($curls,  CURLOPT_RETURNTRANSFER, FALSE);
curl_setopt($curls, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curls, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curls, CURLOPT_POST, true);
curl_setopt($curls, CURLOPT_POSTFIELDS, 'grant_type=client_credentials&client_id={id number}&client_secret={secret}');
curl_setopt($curls, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
$token = curl_exec($curls);
curl_close($curls);
print_r ($token);
1
curl_getinfo() is your friend.Alex Howansky
Kindly make sure that CURLOPT_POSTFIELDS dont require to be json_encodedtphobe9312
curl_error($curls) should tell you what went wrongdelboy1978uk
Thank you guys, your inputs really helped me reach a solution.Lazarus-CG

1 Answers

0
votes

You have a space before your url at the first line.

You can remove some lines as well:

$url = 'https://test.api.amadeus.com/v1/security/oauth2/token';
$curls = curl_init();
curl_setopt($curls, CURLOPT_URL, $url);
curl_setopt($curls, CURLOPT_POST, true);
curl_setopt($curls, CURLOPT_POSTFIELDS, 'grant_type=client_credentials&client_id=API_KEY&client_secret=API_SECRET');
curl_setopt($curls, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
$token = curl_exec($curls);
curl_close($curls);
print_r ($token);