1 回答
TA贡献1848条经验 获得超6个赞
我在您的代码中发现了一些错误并修复了它以使其正常工作:
对于您使用的 PHP 版本,您需要添加
curl_setopt($curls, CURLOPT_RETURNTRANSFER, true);
以确保它不仅打印 API 响应而且还返回它您需要在 Bearer 和访问令牌之间留一个空格:
array('Authorization: Bearer ' .$data['access_token']));
您尝试调用的示例是错误的,返回日期早于出发日期。我将其更改为一个更简单的示例:
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
。在进行 API 调用之前,您需要将 更改
CURLOPT_POST
为 false (因为您尝试调用的 API 是 aGET
)
下面是完整的示例:
$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);
注意:我不是 PHP 专家,我确信代码可以改进。
- 1 回答
- 0 关注
- 148 浏览
添加回答
举报