博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php 获取 ip 信息
阅读量:6993 次
发布时间:2019-06-27

本文共 3754 字,大约阅读时间需要 12 分钟。

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

今天介绍一个开源的

淘宝接口

$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 | phpmv composer.phar /usr/bin/composerchmod +x composercomposer require geoip2/geoip2:~2.0

使用

require_once 'vendor/autoload.php';use GeoIp2\Database\Reader;$reader = new Reader('GeoIP2-City.mmdb');$record = $reader->city('180.149.132.47');// 百度 ipdump($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 . "\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.9289print($record->location->longitude . "\n"); // 116.3883

php binding/php/testSearcher ./data/ip2region.dbp2region>> 101.105.35.572163|中国|华南|广东省|深圳市|鹏博士 in 0.02295 millseconds

公众号:苏生不惑

clipboard.png

转载地址:http://gydvl.baihongyu.com/

你可能感兴趣的文章
批处理
查看>>
packstack工具部署单节点的openstack虚拟服务
查看>>
用户管理
查看>>
mysql运维常用操作
查看>>
我的友情链接
查看>>
python, my first code
查看>>
GPRS发送程序
查看>>
获取浏览器和操作系统类型
查看>>
我的友情链接
查看>>
Java 9终于要包含Jigsaw项目了
查看>>
数据恢复案列
查看>>
django admin找不到STATIC_URL
查看>>
如何通过LayoutInflater,得到多个RadioButton
查看>>
How to hide redirected URL
查看>>
利用SVN的post-commit钩子实现多项目自动同步
查看>>
在SUSE SP3上安装新的python-3.6.4
查看>>
C#高级编程之委托
查看>>
2018.12.7python笔记(3.3-3.5)
查看>>
数据类型之间的运算
查看>>
linux下rar文件的解压
查看>>