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');
~~~
# 安装 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 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"
]
]
}
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
~~~
# ip2region
https://github.com/lionsoul2014/ip2region
~~~
php binding/php/testSearcher ./data/ip2region.db
p2region>> 101.105.35.57
2163|中国|华南|广东省|深圳市|鹏博士 in 0.02295 millseconds
~~~
- OAuth
- 简介
- 步骤
- 单点登录
- .user.ini
- 时间转换为今天昨天前天几天前
- 获取ip接口
- 协程
- 概念
- yield-from && return-values
- 协程与阻塞的思考
- 中间件
- mysqli异步与php的协程
- 代码片段
- pdo 执行的sql语句
- 二进制安全
- 捕捉异常中断
- global
- 利用cookie模拟登陆
- 解析非正常json
- 简单的对称加密算法
- RSA 加密
- 过滤掉emoji表情
- 判断远程图片是否存在
- 一分钟限制请求100次
- 文件处理
- 多文件上传
- 显示所有文件
- 文件下载和上面显示所有文件配合
- 文件的删除,统计,存数组等
- 图片处理
- 简介
- 验证码
- 图片等比缩放
- 批量添加水印
- beanstalkd
- 安装
- 使用
- RabbitMQ
- 简介
- debain安装
- centos安装
- 常用方法
- 入门
- 工作队列
- 订阅,发布
- 路由
- 主题
- 远程调用RPC
- 消息中间件的选型
- .htaccess
- isset、empty、if区别以及0、‘’、null
- php各版本
- php7.2 不向后兼容的改动
- php中的各种坑
- php7改变
- php慢日志
- 邮件
- PHPMailer实现发邮件
- 验证邮件地址真实性
- 文件下载
- FastCgi 与 PHP-fpm 之间的关系
- openssl 加解密
- 反射
- 钩子方法
- 查找插件
- opcode
- opcache使用
- opcache优化
- 分布式一致性hash算法
- 概念
- 哈希算法好坏的四个定义
- php实现
- java实现
- 数组
- jwt
- jwt简介
- 单点登录
- phpize
- GeoIP扩展
- php无法获得https网页内容的解决方案
- homestead运行的脚本
- Unicode和Utf-8转换
- php优化
- kafka
- fpm配置
- configure配置详解