Magento 2 GeoIP Lookup v1.x PHP Interfaces

Using the \Plumrocket\GeoIPLookup\Api\GeoIPLookupInterface Service

The Plumrocket GeoIP Lookup Magento 2 extension allows using the \Plumrocket\GeoIPLookup\Api\GeoIPLookupInterface 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 GeoIPLookupInterface.

Example 1. Get all available geolocation data of a current customer

    private $remoteAddress;
    private $geoIPLookup;

    public function __construct(
        \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress,
        \Plumrocket\GeoIPLookup\Api\GeoIPLookupInterface $geoIPLookup
    ) {
        $this->remoteAddress = $remoteAddress;
        $this->geoIPLookup = $geoIPLookup;
    }

    public function getCurrentCustomerGeoIpData()
    {
        return $this->geoIPLookup->getGeoIpDataWithDefaultServices($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 $geoIPLookup;

public function __construct(
    \Plumrocket\GeoIPLookup\Api\GeoIPLookupInterface $geoIPLookup
) {
    $this->geoIPLookup = $geoIPLookup;
}

public function someFunction()
{
    return $this->geoIPLookup->getGeoIpDataWithDefaultServices('8.8.8.8');
}

Example 3. Get all available data from an IP address, prioritizing the use of GeoIP Lookup services

As an example, we use 8.8.8.8 IP address:

    private $geoIPLookup;

    public function __construct(
        \Plumrocket\GeoIPLookup\Api\GeoIPLookupInterface $geoIPLookup
    ) {
        $this->geoIPLookup = $geoIPLookup;
    }

    public function someFunction()
    {
        $ip = '8.8.8.8';
        $servicePriority = ['ipapi', 'maxmind', 'iptocountry'];
        return $this->geoIPLookup->getGeoIpData($ip, $servicePriority);
    }

By default, the GeoIP 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’];
Was this article helpful?