接入教程
1. 阅读Amadeus授权指南获取访问令牌
2. 准备航班搜索参数
3. 调用API创建订单
4. 处理返回的订单确认信息
5. 在测试环境验证流程
使用Python创建航班订单
python
import requests
import json
# API配置
BASE_URL = 'https://api.example.com'
ENDPOINT = '/v1/booking/flight-orders'
API_KEY = 'YOUR_API_KEY'
# 请求头
headers = {
'Authorization': f'Bearer {API_KEY}',
'Content-Type': 'application/json'
}
# 请求体示例
payload = {
"data": {
"type": "flight-order",
"flightOffers": [{
"type": "flight-offer",
"id": "1"
}],
"travelers": [{
"id": "1",
"dateOfBirth": "1982-01-16",
"name": {
"firstName": "ALEXANDER",
"lastName": "CONRAD"
}
}]
}
}
# 发送POST请求
response = requests.post(
f'{BASE_URL}{ENDPOINT}',
headers=headers,
data=json.dumps(payload)
)
# 处理响应
if response.status_code == 201:
order_data = response.json()
print(f'订单创建成功,ID: {order_data["data"]["id"]}')
else:
print(f'请求失败: {response.status_code}')
print(response.text)
使用PHP创建航班订单
php
<?php
// API配置
$baseUrl = 'https://api.example.com';
$endpoint = '/v1/booking/flight-orders';
$apiKey = 'YOUR_API_KEY';
// 请求头
$headers = [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json'
];
// 请求体示例
$payload = [
'data' => [
'type' => 'flight-order',
'flightOffers' => [
[
'type' => 'flight-offer',
'id' => '1'
]
],
'travelers' => [
[
'id' => '1',
'dateOfBirth' => '1982-01-16',
'name' => [
'firstName' => 'ALEXANDER',
'lastName' => 'CONRAD'
]
]
]
]
];
// 初始化cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $baseUrl . $endpoint);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// 执行请求
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// 处理响应
if ($httpCode == 201) {
$result = json_decode($response, true);
echo '订单创建成功,ID: ' . $result['data']['id'];
} else {
echo '请求失败: ' . $httpCode;
echo $response;
}
?>
使用JavaScript创建航班订单
javascript
const axios = require('axios'); // 或使用fetch API
// API配置
const BASE_URL = 'https://api.example.com';
const ENDPOINT = '/v1/booking/flight-orders';
const API_KEY = 'YOUR_API_KEY';
// 请求头
const headers = {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json'
};
// 请求体示例
const payload = {
"data": {
"type": "flight-order",
"flightOffers": [{
"type": "flight-offer",
"id": "1"
}],
"travelers": [{
"id": "1",
"dateOfBirth": "1982-01-16",
"name": {
"firstName": "ALEXANDER",
"lastName": "CONRAD"
}
}]
}
};
// 发送POST请求
axios.post(`${BASE_URL}${ENDPOINT}`, payload, { headers })
.then(response => {
if (response.status === 201) {
console.log(`订单创建成功,ID: ${response.data.data.id}`);
} else {
console.log(`请求失败: ${response.status}`);
}
})
.catch(error => {
console.error('请求错误:', error.response?.data || error.message);
});
常见问题
如何获取API访问令牌?
使用前需要先阅读Amadeus授权指南获取访问令牌。测试环境基于生产环境的子集,请确保使用正确的环境配置。
请求失败时如何排查问题?
首先检查API密钥和访问令牌是否正确,确认请求头中的Authorization格式为'Bearer YOUR_TOKEN'。然后验证请求体格式是否符合API文档要求,特别是travelers和flightOffers字段。
测试环境和生产环境有什么区别?
测试环境基于生产环境的子集,部分功能可能受限。建议先在测试环境验证集成逻辑,再切换到生产环境。两者使用相同的API端点但需要不同的访问凭证。
Aitishiku.com