接入教程
1. 注册获取API密钥
2. 构造请求URL:https://api.airportsapi.io/airport/{icao}
3. 在请求头中添加认证信息
4. 发送GET请求获取JSON格式的机场数据
5. 解析响应中的name和website字段
通过ICAO代码查询机场信息
python
import requests
url = 'https://api.example.com/airports/{icao}'
headers = {'Authorization': 'Bearer YOUR_API_KEY'}
icao_code = 'EDDF'
response = requests.get(url.format(icao=icao_code), headers=headers)
if response.status_code == 200:
data = response.json()
print(f"Name: {data.get('name')}")
print(f"Website: {data.get('website')}")
else:
print(f"Error: {response.status_code}")
使用PHP获取机场详情
php
<?php
$icao = 'EDDS';
$url = 'https://api.example.com/airports/' . urlencode($icao);
$options = [
'http' => [
'header' => "Authorization: Bearer YOUR_API_KEY\r\n"
]
];
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
if ($response !== false) {
$data = json_decode($response, true);
echo "Name: " . $data['name'] . "\n";
echo "Website: " . $data['website'] . "\n";
} else {
echo "Request failed.\n";
}
?>
JavaScript中调用机场API
javascript
const icao = 'EDDB';
const url = `https://api.example.com/airports/${icao}`;
const options = {
method: 'GET',
headers: {
'Authorization': 'Bearer YOUR_API_KEY'
}
};
fetch(url, options)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
console.log(`Name: ${data.name}`);
console.log(`Website: ${data.website}`);
})
.catch(error => {
console.error('Fetch error:', error);
});
常见问题
该API支持哪些机场?
该API主要覆盖德国境内的机场,使用ICAO代码进行查询。部分其他国家的机场可能也包含在内,但德国机场的数据最为全面。
如何获取API密钥?
请访问API提供商的官方网站进行注册并获取API密钥。在请求时,需将YOUR_API_KEY替换为您的实际密钥。
请求频率是否有限制?
是的,API通常设有请求频率限制以保障服务稳定。具体限制请查阅官方文档,建议合理控制调用频率。
Aitishiku.com