查看: 1640|回复: 0

[PHP学习] php 获取 ip 信息

发表于 2017-8-12 08:00:03
尚学堂AD

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

淘宝接口
  1. $ip = file_get_contents('http://ip.taobao.com/service/getIpInfo.php?ip=180.149.132.47');
  2. print_r(json_decode($ip, 1));
  3. Array
  4. (
  5. [code] => 0
  6. [data] => Array
  7. (
  8. [country] => 中国
  9. [country_id] => CN
  10. [area] => 华北
  11. [area_id] => 100000
  12. [region] => 北京市
  13. [region_id] => 110000
  14. [city] => 北京市
  15. [city_id] => 110100
  16. [county] =>
  17. [county_id] => -1
  18. [isp] => 电信
  19. [isp_id] => 100017
  20. [ip] => 180.149.132.47
  21. )
  22. )
复制代码
纯真 IP 数据库
  1. // 先下文件 QQWry.Dat http://www.cz88.net/fox/ http://update.cz88.net/soft/setup.zip
  2. $IpLocation = new IpLocation('qqwry/QQWry.Dat');
  3. $client = $IpLocation->getlocation('180.149.132.47');
复制代码
安装 geoip
  1. curl -sS https://getcomposer.org/installer | php
  2. mv composer.phar /usr/bin/composer
  3. chmod +x composer
  4. composer require geoip2/geoip2:~2.0
复制代码
使用
  1. require_once 'vendor/autoload.php';
  2. use GeoIp2\Database\Reader;
  3. $reader = new Reader('GeoIP2-City.mmdb');
  4. $record = $reader->city('180.149.132.47');// 百度 ip
  5. dump($record);// dump 来自 laravel
  6. City {#1899 ▼
  7. #city: City {#1908 ?}
  8. #location: Location {#1909 ?}
  9. #postal: Postal {#1910 ?}
  10. #subdivisions: array:1 [?]
  11. #continent: Continent {#1902 ?}
  12. #country: Country {#1903 ?}
  13. #locales: array:1 [?]
  14. #maxmind: MaxMind {#1904 ?}
  15. #registeredCountry: Country {#1905 ?}
  16. #representedCountry: RepresentedCountry {#1906 ?}
  17. #traits: Traits {#1907 ?}
  18. #raw: array:7 [▼
  19. "city" => array:2 [▼
  20. "geoname_id" => 1816670
  21. "names" => array:8 [▼
  22. "de" => "Peking"
  23. "en" => "Beijing"
  24. "es" => "Pekín"
  25. "fr" => "Pékin"
  26. "ja" => "北京市"
  27. "pt-BR" => "Pequim"
  28. "ru" => "Пекин"
  29. "zh-CN" => "北京"
  30. ]
  31. ]
  32. "continent" => array:3 [▼
  33. "code" => "AS"
  34. "geoname_id" => 6255147
  35. "names" => array:8 [▼
  36. "de" => "Asien"
  37. "en" => "Asia"
  38. "es" => "Asia"
  39. "fr" => "Asie"
  40. "ja" => "アジア"
  41. "pt-BR" => "ásia"
  42. "ru" => "Азия"
  43. "zh-CN" => "亚洲"
  44. ]
  45. ]
  46. "country" => array:3 [▼
  47. "geoname_id" => 1814991
  48. "iso_code" => "CN"
  49. "names" => array:8 [▼
  50. "de" => "China"
  51. "en" => "China"
  52. "es" => "China"
  53. "fr" => "Chine"
  54. "ja" => "中国"
  55. "pt-BR" => "China"
  56. "ru" => "Китай"
  57. "zh-CN" => "中国"
  58. ]
  59. ]
  60. "location" => array:4 [▼
  61. "accuracy_radius" => 50
  62. "latitude" => 39.9289
  63. "longitude" => 116.3883
  64. "time_zone" => "Asia/Shanghai"
  65. ]
  66. "registered_country" => array:3 [▼
  67. "geoname_id" => 1814991
  68. "iso_code" => "CN"
  69. "names" => array:8 [▼
  70. "de" => "China"
  71. "en" => "China"
  72. "es" => "China"
  73. "fr" => "Chine"
  74. "ja" => "中国"
  75. "pt-BR" => "China"
  76. "ru" => "Китай"
  77. "zh-CN" => "中国"
  78. ]
  79. ]
  80. "subdivisions" => array:1 [▼
  81. 0 => array:3 [▼
  82. "geoname_id" => 2038349
  83. "iso_code" => "11"
  84. "names" => array:3 [▼
  85. "en" => "Beijing"
  86. "fr" => "Municipalité de Pékin"
  87. "zh-CN" => "北京市"
  88. ]
  89. ]
  90. ]
  91. "traits" => array:1 [▼
  92. "ip_address" => "180.149.132.47"
  93. ]
  94. ]
  95. }
  96. print($record->country->isoCode . "\n"); // 'CN'
  97. print($record->country->name . "\n"); // 'China'
  98. print($record->country->names['zh-CN'] . "\n"); // '中国 '
  99. print($record->mostSpecificSubdivision->name . "\n"); // 'Beijing'
  100. print($record->mostSpecificSubdivision->isoCode . "\n"); // '11'
  101. print($record->city->name . "\n"); // 'Beijing '
  102. print($record->location->latitude . "\n"); // 39.9289
  103. print($record->location->longitude . "\n"); // 116.3883
复制代码
ip2region
  1. php binding/php/testSearcher ./data/ip2region.db
  2. p2region>> 101.105.35.57
  3. 2163|中国|华南|广东省|深圳市|鹏博士 in 0.02295 millseconds
复制代码

公众号:苏生不惑



回复

使用道具 举报