一个流行的 IP 地址获取 API 接口是 ipify.org。它提供一个简单的 GET 请求接口,返回调用者的公网 IP 地址。以下是一个 JavaScript 调用示例:
fetch('https://api.ipify.org?format=json')
.then(response => response.json())
.then(data => console.log('Your IP address is:', data.ip))
.catch(error => console.error('Error:', error));
另一个常见的需求是查询 IP 地址的详细信息,如地理位置、运营商等。一个流行的 API 接口是 ipstack.com。以下是一个 JavaScript 调用示例:
fetch('http://api.ipstack.com/103.96.88.10?access_key=YOUR_ACCESS_KEY')
.then(response => response.json())
.then(data => {
console.log('IP Address:', data.ip);
console.log('Location:', data.city, ',', data.region_name, ',', data.country_name);
console.log('Timezone:', data.timezone_name);
console.log('Latitude:', data.latitude);
console.log('Longitude:', data.longitude);
})
.catch(error => console.error('Error:', error));
请注意,使用 ipstack.com 的 API 需要一个访问密钥,可在其网站上免费注册获取。