简体   繁体   English

Amadeus Flight search api 在 curl php 中传递 access_token 时出错

[英]Amadeus Flight search api getting error while passing access_token in curl php

I am integrating amadeus flight search api and getting error while using access_token.我正在集成 amadeus flight search api 并在使用 access_token 时出错。 I guess problem is at retrieving the access token?我猜问题在于检索访问令牌?

{ "errors": [ { "code": "38191", "title": "Invalid HTTP header", "detail": "Missing or invalid format for mandatory Authorization header", "status": "401" } ] }

I need to get all the flights from source to destination with price details.我需要获取从源到目的地的所有航班以及价格详情。

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

curl_setopt($curls, CURLOPT_URL, 'https://test.api.amadeus.com/v2/shopping/flight-offers?originLocationCode=SYD&destinationLocationCode=BKK&departureDate=2020-10-01&returnDate=2020-08-05&adults=2&includedAirlineCodes=TG&max=3');

curl_setopt($curls, CURLOPT_HTTPHEADER, array('Authorization: Bearer' .$data['access_token']));
$result = curl_exec($curls);
    if (curl_errno($curls)) {
        echo 'Error:' . curl_error($curls);
    }
//print_r ($result);
curl_close ($curls);

I found a few mistakes in your code and fixed it to make it work:我在您的代码中发现了一些错误并修复了它以使其正常工作:

  1. With the version of PHP you use, you need to add curl_setopt($curls, CURLOPT_RETURNTRANSFER, true);随着你使用的PHP版本,需要添加curl_setopt($curls, CURLOPT_RETURNTRANSFER, true); to make sure it doesn't only print the API response but returns it as well (please see this post )确保它不仅打印 API 响应,而且还返回它(请参阅此帖子
  2. You need a space between Bearer and the access token: array('Authorization: Bearer ' .$data['access_token']));您需要在 Bearer 和访问令牌之间留一个空格: array('Authorization: Bearer ' .$data['access_token']));
  3. The example you try to call is wrong, the return date is earlier than the departureDate.您尝试调用的示例是错误的,返回日期早于离开日期。 I changed it for a simpler example: https://test.api.amadeus.com/v2/shopping/flight-offers?originLocationCode=EWR&destinationLocationCode=MIA&departureDate=2020-09-10&returnDate=2020-09-17&adults=1&max=1 .我将其更改为一个更简单的示例: https://test.api.amadeus.com/v2/shopping/flight-offers?originLocationCode=EWR&destinationLocationCode=MIA&departureDate=2020-09-10&returnDate=2020-09-17&adults=1&max=1 Please take a look at the API reference documentatio n to understand the different query parameters you can use.请查看API 参考文档以了解您可以使用的不同查询参数。
  4. Before doing the API call you need change the CURLOPT_POST to false (as the API you are trying to call is a GET )在调用 API 之前,您需要将CURLOPT_POST更改为 false (因为您尝试调用的 API 是GET

Find below the full example:在下面找到完整的例子:

$curls = curl_init();
curl_setopt($curls, CURLOPT_URL, 'https://test.api.amadeus.com/v1/security/oauth2/token');
curl_setopt($curls, CURLOPT_POST, true);
curl_setopt($curls, CURLOPT_POSTFIELDS, "grant_type=client_credentials&client_id=$secretid&client_secret=$secretkey");
curl_setopt($curls, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($curls, CURLOPT_RETURNTRANSFER, true);
$token = curl_exec($curls);
$data = json_decode($token,true);

curl_setopt($curls, CURLOPT_URL, 'https://test.api.amadeus.com/v2/shopping/flight-offers?originLocationCode=EWR&destinationLocationCode=MIA&departureDate=2020-09-10&returnDate=2020-09-17&adults=1&max=1');
curl_setopt($curls, CURLOPT_POST, false);

curl_setopt($curls, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' .$data['access_token']));
$result = curl_exec($curls);
    if (curl_errno($curls)) {
        echo 'Error:' . curl_error($curls);
    }
print_r ($result);
curl_close ($curls);

Note: I am not a PHP expert, I am sure the code can be improved.注意:我不是PHP专家,我相信代码可以改进。

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

相关问题 使用 curl 请求 Amadeus API 访问令牌 - Using curl to request for Amadeus API access token PHP cURL来验证服务器上的Facebook API登录access_token? - PHP cURL to verify Facebook API login access_token on server? 使用PHP cURL获取access_token时,Google OAuth2.0返回“ invalid_request” - Google OAuth2.0 returns 'invalid_request' when getting access_token with PHP cURL 使用Facebook API在PHP和JavaScript中使用Access_token - Access_token in php and javascript with Facebook API 如何使用 PHP curl 刷新 Hootsuite access_token API 服务器到服务器调用 - How to refresh Hootsuite access_token API server-to-server call using PHP curl 在PHP中卷曲以获取access_token OAuth2返回错误:“ invalid_client” - curl in PHP to get access_token OAuth2 return error: “invalid_client” 从PHP中的cURL请求获取Foursquare access_token - get Foursquare access_token from cURL request in PHP OAuth2:错误401在PHP请求中使用access_token(Discord API)未经授权 - OAuth2: Error 401 Unauthorized at PHP request with access_token (Discord API) 在 PHP 中运行 RapidAPI Skyscanner 航班搜索的 POST 创建会话时出现 405 错误 - Getting 405 error while running POST create session of RapidAPI Skyscanner flight search in PHP PHP Twitter Oauth 登录手动获取 access_token - PHP Twitter Oauth login getting access_token manually
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM