Search code examples
phpapicurlaccess-tokenamadeus

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

Solution

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