当前位置: 首页 > 后端技术 > PHP

php获取ip信息

时间:2023-03-29 23:56:31 PHP

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]=>Beijing[city_id]=>110100[county]=>[county_id]=>-1[isp]=>电信[isp_id]=>100017[ip]=>180.149.132.47))pureIP数据库//首先下载文件QQWry.Dathttp://www.cz88.net/fox/http://update.cz88.net/soft/setup.zip$IpLocation=newIpLocation('qqwry/QQWry.Dat');$client=$IpLocation->getlocation('180.149.132.47');安装geoipcurl-sShttps://getcomposer.org/installer|phpmvcomposer.phar/usr/bin/composerchmod+xcomposercomposerrequiregeoip2/geoip2:~2.0使用require_once'vendor/autoload.php';useGeoIp2\Database\Reader;$reader=newReader('GeoIP2-City.mmdb');$record=$reader->city('180.149.132.47');//百度ipdump($record);//dump来自laravelCity{#1899▼#city:City{#1908?}#location:Location{#1909?}#postal:Postal{#1910?}#subdivisions:array:1[?]#continent:大陆{#1902?}#country:国家{#1903?}#locales:array:1[?]#maxmind:MaxMind{#1904?}#registeredCountry:国家{#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[▼"from"=>"Asian""in"=>"Asia""is"=>"Asia""fr"=>Asia""ja"=>"ア""pt-BR"=>"亚洲""父亲"=>"五一""zh-CN"=>"国家"]]"国家"=>array:3[▼"geoname_id"=>1814991"iso_code"=>"CN""names"=>array:8[▼"from"=>"China""in"=>"China""is"=>"China""fr"=>"China""ja"=>"location""pt-BR"=>"China""ru"=>"China""zh-CN"=>"location"]]"location"=>array:4[▼"accuracy_radius"=>50"latitude"=>39.9289"longitude"=>116.3883"time_zone"=>"Asia/Shanghai"]"registered_country"=>array:3[▼"geoname_id"=>iso_code"=>"CN""names"=>array:8[▼"from"=>"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"=>"北京""fr"=>"北京市""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");//'北京'print($record->mostSpecificSubdivision->isoCode."\n");//'11'print($record->city->name."\n");//'北京'print($记录->位置->纬度."\n");//39.9289print($record->location->longitude."\n");//116.3883ip2regionphpbinding/php/testSearcher./data/ip2region.dbp2region>>101.105.35.572163|中国|华南|广东省|深圳|0.02295毫秒彭博士公众号:苏生不糊涂