くるみ
IPアドレスから国や地域の情報を取得したいんだな
- IPアドレスから国名や地域名を取得したい
- GeoIPの使い方は調べたら出てきたが、PHPで利用する方法が知りたい
という方に向けて、PHPでGeoIP2を使ってIPアドレスから国・地域名を取得する方法をまとめました。
ブログ主の備忘録代わりでもありますが、参考になれば幸いです。
お品書き
そもそもGeoIP2とは?
「GeoIP2」はMaxMind社が提供するパッケージ。IPアドレスから国や地名、緯度・経度をはじめとした様々な情報を取得することができます。
無料で使用することができますが、有償版と比べるとスペックでは勿論劣り、更新頻度も低めになっています。
商用で使用する際はライセンスの表記やら何やらをする必要があるね
くるみ
GeoIP2の使い方とIPアドレスから国・地域名を取得する方法
GeoIP2の使い方及びIPアドレスから国・地域名を取得するまでの大まかな手順は以下の通りです。手順
step1
GeoIP2のパッケージをインストールする
step2
データベースファイルをダウンロードする
step3
データベースを読み込む
step4
IPアドレスから地域情報を取得する
手順を押さえたところで、早速本題に移っていきましょう。
step1:GeoIP2のパッケージをインストールする
$ composer require geoip2/geoip2:~2.4
まずはcomposerを利用してGeoIP2のパッケージをインストールしていきます。
2 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
1 package you are using is looking for funding.
Use the `composer fund` command to find out more!
上のようにメッセージが出力されれば完了。
ls -F vendor
autoload.php composer/ geoip2/ maxmind/ maxmind-db/
vendorディレクトリ下がこのようになっていればOK。
step2:データベースファイルをダウンロードする
次に、公式サイトからGeoLite2のDBファイルをダウンロードしていきます。
step1
アカウントを作成する
「Sign Up for GeoLite2」をクリックしアカウントを作成していきます。
必要事項を記入します。画像の通り、メアドまで入力すればOK。
すると入力したアドレス宛にメールが届くので、そこのURLからパスワードを設定し、ログインします。ちなみに今回はお試しで利用するだけなので不要ですが、商用で利用する際などにはライセンスキーの取得が必要になります。
step2
データベースファイルをダウンロードする
まずはダウンロードページに行きます。サイトのフッターメニューから「GeoLite2 Free Geolocation Data」をクリックします。
一度見た画面に来ましたね。今度はその中の「Download Files」をクリックします。
するとダウンロードページに遷移するので、「GeoLite2-City.mmdb」「GeoLite2-Country.mmdb」のどちらかのzipファイルをダウンロードします。今回は国名を取得したいので後者を選択。
step3
解凍し配置する
ダウンロードしたzipファイルを解凍し、mmdb拡張子のDBファイルを適当なパスに配置してあげます。
step3:データベースを読み込む
require 'vendor/autoload.php';
use GeoIp2\Database\Reader;
$reader = new Reader('GeoLite2-Country.mmdb');
そしたらvendor下のautoload.phpを読み込み、Readerクラスのインスタンスを作成します。
step4:IPアドレスから地域情報を取得する
require 'vendor/autoload.php';
use GeoIp2\Database\Reader;
$reader = new Reader('{PATH}/GeoLite2-Country.mmdb');
$ipAddress = '198.143.164.252'; // ja.wordpress.org
var_dump($reader->country($ipAddress));
IPアドレスを引数に指定してcityメソッドを呼んでみます。
object(GeoIp2\Model\City)#4 (12) {
["city":protected]=>
object(GeoIp2\Record\City)#13 (3) {
["validAttributes":protected]=>
array(3) {
[0]=>
string(10) "confidence"
[1]=>
string(9) "geonameId"
[2]=>
string(5) "names"
}
["locales":"GeoIp2\Record\AbstractPlaceRecord":private]=>
array(1) {
[0]=>
string(2) "en"
}
["record":"GeoIp2\Record\AbstractRecord":private]=>
array(0) {
}
}
["location":protected]=>
object(GeoIp2\Record\Location)#14 (2) {
["validAttributes":protected]=>
array(9) {
[0]=>
string(13) "averageIncome"
[1]=>
string(14) "accuracyRadius"
[2]=>
string(8) "latitude"
[3]=>
string(9) "longitude"
[4]=>
string(9) "metroCode"
[5]=>
string(17) "populationDensity"
[6]=>
string(10) "postalCode"
[7]=>
string(16) "postalConfidence"
[8]=>
string(8) "timeZone"
}
["record":"GeoIp2\Record\AbstractRecord":private]=>
array(4) {
["accuracy_radius"]=>
int(1000)
["latitude"]=>
float(37.751)
["longitude"]=>
float(-97.822)
["time_zone"]=>
string(15) "America/Chicago"
}
}
["postal":protected]=>
object(GeoIp2\Record\Postal)#15 (2) {
["validAttributes":protected]=>
array(2) {
[0]=>
string(4) "code"
[1]=>
string(10) "confidence"
}
["record":"GeoIp2\Record\AbstractRecord":private]=>
array(0) {
}
}
["subdivisions":protected]=>
array(0) {
}
["continent":protected]=>
object(GeoIp2\Record\Continent)#7 (3) {
["validAttributes":protected]=>
array(3) {
[0]=>
string(4) "code"
[1]=>
string(9) "geonameId"
[2]=>
string(5) "names"
}
["locales":"GeoIp2\Record\AbstractPlaceRecord":private]=>
array(1) {
[0]=>
string(2) "en"
}
["record":"GeoIp2\Record\AbstractRecord":private]=>
array(3) {
["code"]=>
string(2) "NA"
["geoname_id"]=>
int(6255149)
["names"]=>
array(8) {
["de"]=>
string(11) "Nordamerika"
["en"]=>
string(13) "North America"
["es"]=>
string(13) "Norteamérica"
["fr"]=>
string(17) "Amérique du Nord"
["ja"]=>
string(15) "北アメリカ"
["pt-BR"]=>
string(17) "América do Norte"
["ru"]=>
string(31) "Северная Америка"
["zh-CN"]=>
string(9) "北美洲"
}
}
}
["country":protected]=>
object(GeoIp2\Record\Country)#8 (3) {
["validAttributes":protected]=>
array(5) {
[0]=>
string(10) "confidence"
[1]=>
string(9) "geonameId"
[2]=>
string(17) "isInEuropeanUnion"
[3]=>
string(7) "isoCode"
[4]=>
string(5) "names"
}
["locales":"GeoIp2\Record\AbstractPlaceRecord":private]=>
array(1) {
[0]=>
string(2) "en"
}
["record":"GeoIp2\Record\AbstractRecord":private]=>
array(3) {
["geoname_id"]=>
int(6252001)
["iso_code"]=>
string(2) "US"
["names"]=>
array(8) {
["de"]=>
string(3) "USA"
["en"]=>
string(13) "United States"
["es"]=>
string(14) "Estados Unidos"
["fr"]=>
string(11) "États-Unis"
["ja"]=>
string(21) "アメリカ合衆国"
["pt-BR"]=>
string(14) "Estados Unidos"
["ru"]=>
string(6) "США"
["zh-CN"]=>
string(6) "美国"
}
}
}
["locales":protected]=>
array(1) {
[0]=>
string(2) "en"
}
["maxmind":protected]=>
object(GeoIp2\Record\MaxMind)#9 (2) {
["validAttributes":protected]=>
array(1) {
[0]=>
string(16) "queriesRemaining"
}
["record":"GeoIp2\Record\AbstractRecord":private]=>
array(0) {
}
}
["registeredCountry":protected]=>
object(GeoIp2\Record\Country)#10 (3) {
["validAttributes":protected]=>
array(5) {
[0]=>
string(10) "confidence"
[1]=>
string(9) "geonameId"
[2]=>
string(17) "isInEuropeanUnion"
[3]=>
string(7) "isoCode"
[4]=>
string(5) "names"
}
["locales":"GeoIp2\Record\AbstractPlaceRecord":private]=>
array(1) {
[0]=>
string(2) "en"
}
["record":"GeoIp2\Record\AbstractRecord":private]=>
array(3) {
["geoname_id"]=>
int(6252001)
["iso_code"]=>
string(2) "US"
["names"]=>
array(8) {
["de"]=>
string(3) "USA"
["en"]=>
string(13) "United States"
["es"]=>
string(14) "Estados Unidos"
["fr"]=>
string(11) "États-Unis"
["ja"]=>
string(21) "アメリカ合衆国"
["pt-BR"]=>
string(14) "Estados Unidos"
["ru"]=>
string(6) "США"
["zh-CN"]=>
string(6) "美国"
}
}
}
["representedCountry":protected]=>
object(GeoIp2\Record\RepresentedCountry)#11 (3) {
["validAttributes":protected]=>
array(6) {
[0]=>
string(10) "confidence"
[1]=>
string(9) "geonameId"
[2]=>
string(17) "isInEuropeanUnion"
[3]=>
string(7) "isoCode"
[4]=>
string(5) "names"
[5]=>
string(4) "type"
}
["locales":"GeoIp2\Record\AbstractPlaceRecord":private]=>
array(1) {
[0]=>
string(2) "en"
}
["record":"GeoIp2\Record\AbstractRecord":private]=>
array(0) {
}
}
["traits":protected]=>
object(GeoIp2\Record\Traits)#12 (2) {
["validAttributes":protected]=>
array(20) {
[0]=>
string(22) "autonomousSystemNumber"
[1]=>
string(28) "autonomousSystemOrganization"
[2]=>
string(14) "connectionType"
[3]=>
string(6) "domain"
[4]=>
string(9) "ipAddress"
[5]=>
string(11) "isAnonymous"
[6]=>
string(16) "isAnonymousProxy"
[7]=>
string(14) "isAnonymousVpn"
[8]=>
string(17) "isHostingProvider"
[9]=>
string(17) "isLegitimateProxy"
[10]=>
string(3) "isp"
[11]=>
string(13) "isPublicProxy"
[12]=>
string(18) "isResidentialProxy"
[13]=>
string(19) "isSatelliteProvider"
[14]=>
string(13) "isTorExitNode"
[15]=>
string(7) "network"
[16]=>
string(12) "organization"
[17]=>
string(13) "staticIpScore"
[18]=>
string(9) "userCount"
[19]=>
string(8) "userType"
}
["record":"GeoIp2\Record\AbstractRecord":private]=>
array(3) {
["ip_address"]=>
string(15) "198.143.164.252"
["prefix_len"]=>
int(20)
["network"]=>
string(16) "198.143.160.0/20"
}
}
["raw":protected]=>
array(5) {
["continent"]=>
array(3) {
["code"]=>
string(2) "NA"
["geoname_id"]=>
int(6255149)
["names"]=>
array(8) {
["de"]=>
string(11) "Nordamerika"
["en"]=>
string(13) "North America"
["es"]=>
string(13) "Norteamérica"
["fr"]=>
string(17) "Amérique du Nord"
["ja"]=>
string(15) "北アメリカ"
["pt-BR"]=>
string(17) "América do Norte"
["ru"]=>
string(31) "Северная Америка"
["zh-CN"]=>
string(9) "北美洲"
}
}
["country"]=>
array(3) {
["geoname_id"]=>
int(6252001)
["iso_code"]=>
string(2) "US"
["names"]=>
array(8) {
["de"]=>
string(3) "USA"
["en"]=>
string(13) "United States"
["es"]=>
string(14) "Estados Unidos"
["fr"]=>
string(11) "États-Unis"
["ja"]=>
string(21) "アメリカ合衆国"
["pt-BR"]=>
string(14) "Estados Unidos"
["ru"]=>
string(6) "США"
["zh-CN"]=>
string(6) "美国"
}
}
["location"]=>
array(4) {
["accuracy_radius"]=>
int(1000)
["latitude"]=>
float(37.751)
["longitude"]=>
float(-97.822)
["time_zone"]=>
string(15) "America/Chicago"
}
["registered_country"]=>
array(3) {
["geoname_id"]=>
int(6252001)
["iso_code"]=>
string(2) "US"
["names"]=>
array(8) {
["de"]=>
string(3) "USA"
["en"]=>
string(13) "United States"
["es"]=>
string(14) "Estados Unidos"
["fr"]=>
string(11) "États-Unis"
["ja"]=>
string(21) "アメリカ合衆国"
["pt-BR"]=>
string(14) "Estados Unidos"
["ru"]=>
string(6) "США"
["zh-CN"]=>
string(6) "美国"
}
}
["traits"]=>
array(2) {
["ip_address"]=>
string(15) "198.143.164.252"
["prefix_len"]=>
int(20)
}
}
}
オブジェクトの中身がずらっと出力されました。国名なども含まれていますね。
require 'vendor/autoload.php';
use GeoIp2\Database\Reader;
$reader = new Reader('{PATH}/GeoLite2-Country.mmdb');
$ipAddress = '198.143.164.252'; // ja.wordpress.org
var_dump($reader->country($ipAddress)->country->names['ja']);
てことでプロパティ名を指定して日本語で国名を出力してみます。
string(21) "アメリカ合衆国"
無事、国名が取得できました。
お疲れ様!
くるみ
まとめ
参考になれば幸いです!では⸝⸝- ̫ -⸝⸝
参考 GeoIP2 PHP APIGitHub