该API接口提供以下功能:
接口地址: https://api.example.com/v1/ip
参数名称 | 参数类型 | 是否必填 | 说明 |
---|---|---|---|
ip | string | 是 | 需要查询的IP地址,支持单个IP地址或多个IP地址,多个IP地址以逗号分隔 |
format | string | 否 | 返回数据的格式,可选值为json或xml,默认为json |
API接口返回的数据格式为JSON或XML,具体取决于请求时设置的format参数。以下为JSON格式的返回数据示例:
{
"data": [
{
"ip": "8.8.8.8",
"country": "United States",
"region": "California",
"city": "Mountain View",
"latitude": 37.4192,
"longitude": -122.0574,
"isp": "Google LLC"
},
{
"ip": "114.114.114.114",
"country": "China",
"region": "Beijing",
"city": "Beijing",
"latitude": 39.9075,
"longitude": 116.3972,
"isp": "China Unicom"
}
]
}
请求失败,API接口将返回相应的错误信息,包括错误代码和错误描述。例如:
{
"error": {
"code": 400,
"message": "Invalid IP address format"
}
}
以下是使用Python的requests库进行API调用的示例代码:
import requests
url = "https://api.example.com/v1/ip"
params = {
"ip": "8.8.8.8,114.114.114.114"
}
response = requests.get(url, params=params)
if response.status_code == 200:
data = response.json()
print(data)
else:
error = response.json()
print(f"Error {error['error']['code']}: {error['error']['message']}")