PHP 获取 ip 地址信息之前用过新浪/淘宝的接口,但如果新浪/淘宝接口出问题就没法用了,而且网络获取也慢. 今天介绍一个开源的 geoip https://github.com/maxmind/GeoIP2-php

淘宝接口

$ip = file_get_contents('http://ip.taobao.com/service/getIpInfo.php?ip=180.149.132.47');

print_r(json_decode($ip, 1));

Array

(

   [code] => 0

   [data] => Array

       (

           [country] => 中国

           [country_id] => CN

           [area] => 华北

           [area_id] => 100000

           [region] => 北京市

           [region_id] => 110000

           [city] => 北京市

           [city_id] => 110100

           [county] =>

           [county_id] => -1

           [isp] => 电信

           [isp_id] => 100017

           [ip] => 180.149.132.47

       )

)



纯真 IP 数据库

// 先下文件 QQWry.Dat http://www.cz88.net/fox/  http://update.cz88.net/soft/setup.zip

$IpLocation = new IpLocation('qqwry/QQWry.Dat');

$client = $IpLocation->getlocation('180.149.132.47');



安装

curl -sS https://getcomposer.org/installer | php

mv composer.phar /usr/bin/composer

chmod +x composer

composer require geoip2/geoip2:~2.0



使用

  1. require_once 'vendor/autoload.php';
    
    use GeoIp2\Database\Reader;
    
    $reader = new Reader('GeoIP2-City.mmdb');
    
    $record = $reader->city('180.149.132.47');// 百度 ip
    
    dump($record);// dump 来自 laravel
    
    City {#1899 ▼
    
     #city: City {#1908 ▶}
    
     #location: Location {#1909 ▶}
    
     #postal: Postal {#1910 ▶}
    
     #subdivisions: array:1 [▶]
    
     #continent: Continent {#1902 ▶}
    
     #country: Country {#1903 ▶}
    
     #locales: array:1 [▶]
    
     #maxmind: MaxMind {#1904 ▶}
    
     #registeredCountry: Country {#1905 ▶}
    
     #representedCountry: RepresentedCountry {#1906 ▶}
    
     #traits: Traits {#1907 ▶}
    
     #raw: array:7 [▼
    
       "city" => array:2 [▼
    
         "geoname_id" => 1816670
    
         "names" => array:8 [▼
    
           "de" => "Peking"
    
           "en" => "Beijing"
    
           "es" => "Pekín"
    
           "fr" => "Pékin"
    
           "ja" => "北京市"
    
           "pt-BR" => "Pequim"
    
           "ru" => "Пекин"
    
           "zh-CN" => "北京"
    
         ]
    
       ]
    
       "continent" => array:3 [▼
    
         "code" => "AS"
    
         "geoname_id" => 6255147
    
         "names" => array:8 [▼
    
           "de" => "Asien"
    
           "en" => "Asia"
    
           "es" => "Asia"
    
           "fr" => "Asie"
    
           "ja" => "アジア"
    
           "pt-BR" => "Ásia"
    
           "ru" => "Азия"
    
           "zh-CN" => "亚洲"
    
         ]
    
       ]
    
       "country" => array:3 [▼
    
         "geoname_id" => 1814991
    
         "iso_code" => "CN"
    
         "names" => array:8 [▼
    
           "de" => "China"
    
           "en" => "China"
    
           "es" => "China"
    
           "fr" => "Chine"
    
           "ja" => "中国"
    
           "pt-BR" => "China"
    
           "ru" => "Китай"
    
           "zh-CN" => "中国"
    
         ]
    
       ]
    
       "location" => array:4 [▼
    
         "accuracy_radius" => 50
    
         "latitude" => 39.9289
    
         "longitude" => 116.3883
    
         "time_zone" => "Asia/Shanghai"
    
       ]
    
       "registered_country" => array:3 [▼
    
         "geoname_id" => 1814991
    
         "iso_code" => "CN"
    
         "names" => array:8 [▼
    
           "de" => "China"
    
           "en" => "China"
    
           "es" => "China"
    
           "fr" => "Chine"
    
           "ja" => "中国"
    
           "pt-BR" => "China"
    
           "ru" => "Китай"
    
           "zh-CN" => "中国"
    
         ]
    
       ]
    
       "subdivisions" => array:1 [▼
    
         0 => array:3 [▼
    
           "geoname_id" => 2038349
    
           "iso_code" => "11"
    
           "names" => array:3 [▼
    
             "en" => "Beijing"
    
             "fr" => "Municipalité de Pékin"
    
             "zh-CN" => "北京市"
    
           ]
    
         ]
    
       ]
    
       "traits" => array:1 [▼
    
         "ip_address" => "180.149.132.47"
    
       ]
    
     ]
    
    }



  2. print($record->country->isoCode . "\n"); // 'CN'
    
    print($record->country->name . "\n"); // 'China'
    
    print($record->country->names['zh-CN'] . "\n"); // '中国 '
    
    print($record->mostSpecificSubdivision->name . "\n"); // 'Beijing'
    
    print($record->mostSpecificSubdivision->isoCode . "\n"); // '11'
    
    print($record->city->name . "\n"); // 'Beijing '
    
    print($record->location->latitude . "\n"); // 39.9289
    
    print($record->location->longitude . "\n"); // 116.3883