资讯专栏INFORMATION COLUMN

php 获取 ip 信息

TZLLOG / 1797人阅读

摘要:获取地址信息之前用过新浪淘宝的接口,但如果新浪淘宝接口出问题就没法用了,而且网络获取也慢今天介绍一个开源的淘宝接口中国华北北京市北京市电信纯真数据库先下文件安装使用百度来自北京市北京亚洲中国中国中国中国北京市

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

淘宝接口
$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");
安装 geoip
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/bin/composer
chmod +x composer
composer require geoip2/geoip2:~2.0
使用
require_once "vendor/autoload.php";
use GeoIp2DatabaseReader;
$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"
    ]
  ]
}

print($record->country->isoCode . "
"); // "CN"
print($record->country->name . "
"); // "China"
print($record->country->names["zh-CN"] . "
"); // "中国 "
print($record->mostSpecificSubdivision->name . "
"); // "Beijing"
print($record->mostSpecificSubdivision->isoCode . "
"); // "11"
print($record->city->name . "
"); // "Beijing "
print($record->location->latitude . "
"); // 39.9289
print($record->location->longitude . "
"); // 116.3883
ip2region
php binding/php/testSearcher ./data/ip2region.db
p2region>> 101.105.35.57
2163|中国|华南|广东省|深圳市|鹏博士 in 0.02295 millseconds

公众号:苏生不惑

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/23346.html

相关文章

  • Nginx前端设置反向代理,后端Apache如何获取访客的真实IP,结合PHP

    摘要:总结在作为反向代理的架构中,的其他语言也是类似的名称拿到的将会是代理的地址。本意就是远程的地址,是代理层,转发请求到,获取到的远程地址实际上是反向代理服务器,这是符合协议规则的。 nginx反向代理后,在应用中取得的ip都是反向代理服务器的ip,取得的域名也是反向代理配置的url的域名,解决该问题,需要在nginx反向代理配置中添加一些配置信息,目的将客户端的真实ip和域名传递到应用程...

    OpenDigg 评论0 收藏0
  • Nginx前端设置反向代理,后端Apache如何获取访客的真实IP,结合PHP

    摘要:总结在作为反向代理的架构中,的其他语言也是类似的名称拿到的将会是代理的地址。本意就是远程的地址,是代理层,转发请求到,获取到的远程地址实际上是反向代理服务器,这是符合协议规则的。 nginx反向代理后,在应用中取得的ip都是反向代理服务器的ip,取得的域名也是反向代理配置的url的域名,解决该问题,需要在nginx反向代理配置中添加一些配置信息,目的将客户端的真实ip和域名传递到应用程...

    Richard_Gao 评论0 收藏0
  • PHP获取IP地址以及IP地址所在位置

    摘要:获取地址新浪接口根据查询所在区域信息淘宝接口根据查询所在区域信息 获取IP地址: function getIP(){ if (isset($_SERVER)) { if (isset($_SERVER[HTTP_X_FORWARDED_FOR])) { $realip = $_SERVER[HTTP_X_FORWARDED_FOR]; ...

    马永翠 评论0 收藏0
  • PHP 根据ip获取对应的实际地址

    在网上有很多的根据ip地址获取实际地址的教程 他们给了很多的现成接口,例: 新浪:http://int.dpool.sina.com.cn/... . $ip 淘宝:http://ip.taobao.com/service/getIpInfo.php?ip=.$ip 等接口去根据ip地址获取实际地址的接口,但是这些接口十分的不稳定,这里我推荐百度地图的根据ip获取实际地址的接口,经过测试,速度杠杠...

    awesome23 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<