騰訊的接口是 ,返回數組 http://fw.qq.com/ipaddress
返回值 var IPData = new Array("61.135.152.194","","北京市","");
新浪的接口 : http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js
多地域測試方法:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=218.192.3.42
返回值 var remote_ip_info = {"ret":1,"start":"218.192.0.0","end":"218.192.7.255","country":"\u4e2d\u56fd","province":"\u5e7f\u4e1c","city":"\u5e7f\u5dde","district":"","isp":"\u6559\u80b2\u7f51","type":"\u5b66\u6821","desc":"\u5e7f\u5dde\u5927\u5b66\u7eba\u7ec7\u670d\u88c5\u5b66\u9662"};
使用
騰迅的api接口,php獲取ip地址以及所在城市
http://fw.qq.com/ipaddress返回類似:var IPData = new Array("61.51.71.183","","北京市","");
代碼
public static function positionAction($ip) { $ch = curl_init("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip={$ip}"); //curl_setopt($ch,CURLOPT_ENCODING ,'utf8'); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 獲取數據返回 $location = json_decode(curl_exec($ch)); curl_close($ch); if ($location->ret == -1) { return [ 'status' => 0, 'info' => '獲取地理位置失敗' ]; } return [ 'status' => 1, 'info' => '獲取地理位置成功', 'handle' => $location ]; }
php用淘寶接口獲取ip的城市省份
/** * 通過淘寶IP接口獲取IP地理位置 * @param string $ip * @return: string **/ function getCity($ip) { $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip; $ipinfo=json_decode(file_get_contents($url)); if($ipinfo->code=='1'){ return false; } $city = $ipinfo->data->region.$ipinfo->data->city; return $city; } header("Content-Type:text/html;charset=utf-8"); // 這樣調用,顯示山東省臨沂市 var_dump(getCity("112.234.69.189")); ?>