根据ip地址和经纬度获取其所在省市区
请求参数说明:
名称 | 必填 | 类型 | 说明 |
---|---|---|---|
json | 1 | String | 值为true |
coords | 1 | String | 经度纬度都是2个带小数点的数字,且是国内区域 |
ip | 0 | String | 指定IP地址,否则为本地ip |
返回参数说明:
名称 | 类型 | 说明 |
---|
json返回示例:
{
"pro": "广东省",
"proCode": "440000",
"city": "广州市",
"cityCode": "440100",
"region": "",
"regionCode": "",
"error": ""
}
错误码格式说明:
名称 | 类型 | 说明 |
---|
<?php
//----------------------------------
// 太平洋网络 IP经纬度查询 调用类
//----------------------------------
class freeApi{
private $apiUrl = 'http://whois.pconline.com.cn/ipAreaCoordJson.jsp?json=true&coords=113.360414,23.16405';
/**
* 获取 IP经纬度查询 结果
* @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;
}
}