根据IP获取城市-新浪ip接口

 /**
  +----------------------------------------------------------
 * content : 获取IP地址
 * time      : 2018年7月9日
  +----------------------------------------------------------
 */
public function GetIp() {
        $realip = '';
        $unknown = 'unknown';
        if (isset($_SERVER)) {
            if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR']) && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], $unknown)) {
                $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
                foreach ($arr as $ip) {
                    $ip = trim($ip);
                    if ($ip != 'unknown') {
                        $realip = $ip;
                        break;
                    }
                }
            } else if (isset($_SERVER['HTTP_CLIENT_IP']) && !empty($_SERVER['HTTP_CLIENT_IP']) && strcasecmp($_SERVER['HTTP_CLIENT_IP'], $unknown)) {
                $realip = $_SERVER['HTTP_CLIENT_IP'];
            } else if (isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR']) && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown)) {
                $realip = $_SERVER['REMOTE_ADDR'];
            } else {
                $realip = $unknown;
            }
        } else {
            if (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), $unknown)) {
                $realip = getenv("HTTP_X_FORWARDED_FOR");
            } else if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), $unknown)) {
                $realip = getenv("HTTP_CLIENT_IP");
            } else if (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), $unknown)) {
                $realip = getenv("REMOTE_ADDR");
            } else {
                $realip = $unknown;
            }
        }
        $realip = preg_match("/[\d\.]{7,15}/", $realip, $matches) ? $matches[0] : $unknown;
        return $realip;
    }
/**
  +----------------------------------------------------------
 * content : 根据IP获取城市 
 * time      : 2016年12月16日
 * 备注 :这是原新浪的ip接口 现在已经无法使用了 可以使用函数GetIpLookup2
  +----------------------------------------------------------
 */
public function GetIpLookup($ip = '') {
        if (empty($ip)) {
            $ip = $this->GetIp();
        }
        $res = @file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=' . $ip);
        if (empty($res)) {
            return false;
        }
        $jsonMatches = array();
        preg_match('#\{.+?\}#', $res, $jsonMatches);
        if (!isset($jsonMatches[0])) {
            return false;
        }
        $json = json_decode($jsonMatches[0], true);
        if (isset($json['ret']) && $json['ret'] == 1) {
            $json['ip'] = $ip;
            unset($json['ret']);
        } else {
            return false;
        }
        return $json;
    }

/**
  +----------------------------------------------------------
 * content : 根据IP获取城市 
 * time      : 2016年12月16日
 * 备注 :淘宝IP接口
  +----------------------------------------------------------
 */
public function GetIpLookup2($ip = '') {
        if (empty($ip)) {
            $ip = $this->GetIp();
        }
        //淘宝ip接口
        $res = @file_get_contents('http://ip.taobao.com/service/getIpInfo.php?ip=' . $ip);
        if (empty($res)) {
            return false;
        }
        $json = json_decode($res, true);
        if (isset($json['code']) && $json['code'] == 0) {
            $json['ip'] = $ip;
            unset($json['ret']);
        } else {
            return false;
        }
        return $json['data'];
    }

IP地址查询接口及调用方法

  • 设计蜂巢IP地址查询接口:http://www.hujuntao.com/api/ip/ip.php
  • 腾讯IP地址查询接口:http://fw.qq.com/ipaddress
  • 新浪IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js
  • 搜狐IP地址查询接口:http://pv.sohu.com/cityjson
  • 谷歌IP地址查询接口:http://j.maxmind.com/app/geoip.js
  • 有道IP地址查询接口:http://www.youdao.com/smartresult-xml/search.s
  • 1616 IP地址查询接口:http://w.1616.net/chaxun/iptolocal.php
  • 126 http://ip.ws.126.net/ipquery
  • hao123 http://app.hao123.com/ipquery/getcity.php?rtype=2
  • 淘宝 http://ip.taobao.com/service/getIpInfo.php?ip=117.89.35.58
  • 太平洋电脑网 http://whois.pconline.com.cn/

你可能感兴趣的:(PHP)