接入教程
1. 阅读Amadeus授权指南获取访问令牌
2. 使用令牌调用酒店搜索API端点
3. 解析返回的JSON数据获取酒店列表
4. 根据需要筛选和展示酒店信息
5. 在生产环境部署前进行充分测试
Python示例:搜索酒店
python
import requests
url = "https://api.example.com/v1/hotels/search"
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
payload = {
"cityCode": "NYC",
"checkInDate": "2023-12-01",
"checkOutDate": "2023-12-05",
"roomQuantity": 1
}
response = requests.post(url, json=payload, headers=headers)
if response.status_code == 200:
hotels = response.json()
print(f"Found {len(hotels.get('data', []))} hotels")
else:
print(f"Error: {response.status_code}", response.text)
PHP示例:获取酒店详情
php
<?php
$url = 'https://api.example.com/v1/hotels/details';
$apiKey = 'YOUR_API_KEY';
$data = [
'hotelId' => '12345',
'adults' => 2
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode == 200) {
$result = json_decode($response, true);
echo "Hotel: " . $result['name'] . "\n";
} else {
echo "Error: " . $httpCode . "\n";
echo $response;
}
?>
JavaScript示例:按城市查询酒店
javascript
const fetch = require('node-fetch');
async function searchHotels() {
const url = 'https://api.example.com/v1/hotels/by-city';
const options = {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
cityCode: 'LON',
radius: 5,
radiusUnit: 'KM'
})
};
try {
const response = await fetch(url, options);
if (response.ok) {
const data = await response.json();
console.log(`Found ${data.hotels.length} hotels in London`);
} else {
console.error(`HTTP Error: ${response.status}`);
}
} catch (error) {
console.error('Request failed:', error);
}
}
searchHotels();
常见问题
如何获取API访问令牌?
请参考官方授权指南生成访问令牌,测试环境需使用测试密钥,生产环境需申请正式密钥。
测试环境和生产环境有什么区别?
测试环境基于生产环境的子集,数据可能不完整或延迟更新,主要用于功能验证。
API支持哪些搜索参数?
支持城市代码、入住日期、离店日期、房间数量等基本参数,具体请查阅API文档。
Aitishiku.com