接入教程
访问官网注册获取API密钥,使用REST API接口查询航班数据。免费计划每月提供100次API调用,支持实时航班追踪、历史数据查询等功能。
使用Python查询航班状态
python
import requests
url = "https://api.example.com/v1/flights"
params = {
"access_key": "YOUR_API_KEY",
"flight_status": "active",
"limit": 10
}
response = requests.get(url, params=params)
data = response.json()
if data.get('data'):
for flight in data['data']:
print(f"Flight: {flight.get('flight', {}).get('iata')} Status: {flight.get('flight_status')}")
else:
print("Error:", data.get('error', {}).get('message'))
使用PHP获取机场信息
php
<?php
$url = "https://api.example.com/v1/airports";
$params = [
"access_key" => "YOUR_API_KEY",
"search" => "JFK",
"limit" => 5
];
$ch = curl_init($url . '?' . http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
if (isset($data['data'])) {
foreach ($data['data'] as $airport) {
echo "Airport: " . $airport['airport_name'] . " (" . $airport['iata_code'] . ")\n";
}
} else {
echo "Error: " . ($data['error']['message'] ?? 'Unknown error');
}
?>
使用JavaScript实时追踪航班
javascript
const url = 'https://api.example.com/v1/flights';
const params = new URLSearchParams({
access_key: 'YOUR_API_KEY',
flight_iata: 'CA981',
flight_status: 'enroute'
});
fetch(`${url}?${params}`)
.then(response => response.json())
.then(data => {
if (data.data && data.data.length > 0) {
const flight = data.data[0];
console.log(`Flight ${flight.flight.iata} is ${flight.flight_status}`);
console.log(`Departure: ${flight.departure.airport}, Arrival: ${flight.arrival.airport}`);
} else {
console.log('Error:', data.error?.message || 'No flight data');
}
})
.catch(error => console.error('Request failed:', error));
常见问题
如何获取API访问密钥?
请访问Aviationstack官网注册账户,在控制面板中可以生成您的专属API密钥。免费套餐提供有限的请求次数,付费套餐可享受更多功能和更高请求限额。
API请求频率有限制吗?
是的,API请求频率根据您的订阅套餐有所不同。免费套餐通常有每分钟和每月的请求限制。建议在代码中添加适当的延迟和错误处理,避免超出限制。
支持哪些数据查询功能?
Aviationstack API提供航班实时状态查询、历史航班数据、航班时刻表、航空公司信息、机场详情、飞机数据、航线信息等多种航空数据服务。具体端点请参考官方文档。
Aitishiku.com