简体   繁体   中英

Using curl to request for Amadeus API access token

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);

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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