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配置詳解