接口地址:http://ip-api.com/json |
---|
返回格式:json |
请求方式:get/post |
请求示例:http://ip-api.com/json/24.48.0.1?lang=zh-CN |
名称 | 类型 | 必填 | 说明 |
---|---|---|---|
fields | string | 选填 | 字段 |
lang | string | 选填 | 语言 |
callback | string | 选填 | 回调函数 |
名称 | 类型 | 说明 |
---|---|---|
status | string | 返回状态:success 成功 ,fail 失败 |
message | string | 仅在状态为失败的时候显示 |
continent | string | 大陆名称 |
continentCode | string | 大陆代号 |
country | string | 国家的名称 |
countryCode | string | 国家代号 |
region | string | 区域 |
regionName | string | 地区国家 |
city | string | 城市 |
district | string | 地区城市明细 |
zip | string | 邮编 |
lat | float | 纬度 |
lon | float | 经度 |
timezone | string | 时区 |
currency | string | 本国货币 |
isp | string | 互联网服务提供商 |
org | string | 组织名称 |
as | string | as编号 |
asname | string | as名称 |
{
"as": "AS5769 Videotron Telecom Ltee",
"city": "蒙特利尔",
"country": "加拿大",
"countryCode": "CA",
"isp": "Le Groupe Videotron Ltee",
"lat": 45.5808,
"lon": -73.5825,
"org": "Videotron Ltee",
"query": "24.48.0.1",
"region": "QC",
"regionName": "Quebec",
"status": "success",
"timezone": "America/Toronto",
"zip": "H1S"
}
错误码 | 说明 |
---|---|
- | - |
<?php
/**
* Created by PhpStorm.
* User: FZS
* Time: 2019/9/3 23:10
*/
//----------------------------------
// IP地理位置 调用类
//----------------------------------
class freeApi{
private $apiUrl;
public function __construct(){
$this->apiUrl = 'http://ip-api.com/json/24.48.0.1?lang=zh-CN';
}
/**
* 获取结果
* @return array
*/
public function getResult(){
return $this->freeApiCurl($this->apiUrl);
}
/**
* 请求接口返回内容
* @param string $url [请求的URL地址]
* @param string $params [请求的参数]
* @param int $ipost [是否采用POST形式]
* @return string
*/
public function freeApiCurl($url,$params=false,$ispost=0){
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
curl_setopt( $ch, CURLOPT_USERAGENT , 'free-api' );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
if( $ispost )
{
curl_setopt( $ch , CURLOPT_POST , true );
curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
curl_setopt( $ch , CURLOPT_URL , $url );
}
else
{
if($params){
curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
}else{
curl_setopt( $ch , CURLOPT_URL , $url);
}
}
$response = curl_exec( $ch );
if ($response === FALSE) {
return false;
}
curl_close( $ch );
return $response;
}
}