php geolocation
v2.0.1
이 패키지는 IP 주소의 국가를 결정하는 PHP 라이브러리를 제공합니다.
작곡가로 PHP 지리적 위치를 쉽게 설치할 수 있습니다.
composer require divineomega/php-geolocation
PHP 지리적 위치의 가장 간단한 사용은 새로운 로케이터 객체를 만들고 getCountryByIP 방법을 호출하는 것입니다.
// Get country of the current request's IP address
$ country = ( new Locator )-> getCountryByIP ( $ _SERVER [ ' REMOTE_ADDR ' ]);
// Get country of a specific IP address
$ country = ( new Locator )-> getCountryByIP ( ' 93.184.216.34 ' );
// Returns a Country object
/*
object(DivineOmegaCountriesCountry)#4693 (16) {
["name"]=>
string(13) "United States"
["officialName"]=>
string(24) "United States of America"
// etc...
}
*/ PSR-6 호환 캐싱 라이브러리를 사용하도록 PHP Geolocation을 구성 할 수 있습니다. 이것은 setCache 메소드를 사용하여 쉽게 수행됩니다.
다음 예제는 파일 캐시를 구성합니다 ( cache/filesystem-adapter 패키지에서 제공).
use League Flysystem Adapter Local ;
use League Flysystem Filesystem ;
use Cache Adapter Filesystem FilesystemCachePool ;
$ filesystemAdapter = new Local ( __DIR__ . ' / ' );
$ filesystem = new Filesystem ( $ filesystemAdapter );
$ cachePool = new FilesystemCachePool ( $ filesystem );
$ locator = new Locator ;
$ locator -> setCache ( $ cachePool );
$ country = $ locator -> getCountryByIP ( ' 93.184.216.34 ' ); 기본적으로 PHP Geolocation은 운영 체제의 기본 whois 명령을 사용하여 IP 주소를 결정합니다. 원하는 경우 대체 위치 제공 업체를 사용할 수 있습니다. 다음과 같이 setLocationProvider 메소드를 사용하여 수행 할 수 있습니다.
$ locator = new Locator ;
$ locator -> setLocationProvider ( new IpStack ( ' my_ip_stack_api_key ' );
$ country = $ locator -> getCountryByIP ( ' 93.184.216.34 ' );IP STACK 웹 사이트에서 무료 API 키를 받으려면
자신의 위치 제공 업체를 개발하려면이 패키지에 제공된 LocationProviderInterface 구현하는 새로운 클래스를 작성하십시오. 자신의 위치 제공 업체를 만드는 데 도움이 필요한 경우 기존 WhoIs 및 FreeGeoIP 위치 제공 업체 수업을 참조하십시오.