Using the \Plumrocket\IPGeolocationLookup\Api\IPGeolocationLookupInterface Service
The Plumrocket IP Geolocation Lookup Magento 2 extension allows using the \Plumrocket\IPGeolocationLookup\Api\IPGeolocationLookupInterface service in order to receive the customer geolocation data from the IP address. We have provided you with three examples of the most common uses of the IPGeolocationLookupInterface.
Example 1. Get all available geolocation data of a current customer
private $remoteAddress;
private $IPGeolocationLookup;
public function __construct(
\Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress,
\Plumrocket\GeoIPLookup\Api\IPGeolocationLookupInterface $IPGeolocationLookup
) {
$this->remoteAddress = $remoteAddress;
$this->IPGeolocationLookup = $geoIPLookup;
}
public function getCurrentCustomerGeoIpData()
{
return $this->IPGeolocationLookup->getIPGeolocationDataWithDefaultServices($this->remoteAddress->getRemoteAddress());
}Example 2. Get all available geolocation data of an IP address
As an example, we use the 8.8.8.8 IP address:
private $IPgeolocationLookup;
public function __construct(
\Plumrocket\IPGeolocationLookup\Api\GeoIPLookupInterface $geoIPLookup
) {
$this->gIPGeolocationLookup = $IPGeolocationLookup;
}
public function someFunction()
{
return $this->IPGeolocationLookup->getIPGeolocationDataWithDefaultServices('8.8.8.8');
}Example 3. Get all available data from an IP address, prioritizing the use of IP Geolocation Lookup services
As an example, we use 8.8.8.8 IP address:
private $IPGeolocationLookup;
public function __construct(
\Plumrocket\IPGeolocationLookup\Api\IPGeolocationLookupInterface $IPgeolocationLookup
) {
$this->IPgeolocationLookup = $IPGeolocationLookup;
}
public function someFunction()
{
$ip = '8.8.8.8';
$servicePriority = ['ipapi', 'maxmind', 'iptocountry'];
return $this->IPgeolocationLookup->getIPgeolocationData($ip, $servicePriority);
}By default, the IP Geolocation Lookup extension uses the services in the following priority: Maxmind, IpToCountry, IpApi. However, you can specify any other priority you want, sorting elements in the following array:
$servicePriority = ['ipapi', 'maxmind', 'iptocountry']; You can also specify only one or two elements in an array, for instance:
1) $servicePriority = ['ipapi'];
2) $servicePriority = ['iptocountry', ‘maxmind’];