Magento 2.2.2 - Maxmind GeoIP2 WebService

by Vikram   Last Updated August 07, 2018 15:09 PM

I am using this extension to use Maxmind GeoIP2 WebService and below is the issue. I have configured below details in as per my maxmind account. I am using web services client in order to fetch location(country/state/city) based on IP

  1. UserID - maxmind account userid
  2. License Key - maxmind account license key
  3. Host - geoip.maxmind.com

Below is the code in my simple GeoIP module controller. It is not giving me any output when I run the code

<?php

namespace Namespace\Modulename\Controller\Index;

use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Tobai\GeoIp2\Model\WebService\ClientFactory;

class Index extends \Magento\Framework\App\Action\Action
{

    protected $resultPageFactory;
    protected $clientFactory;

    public function __construct(Context $context, PageFactory $pageFactory, ClientFactory $clientFactory)
    {
        $this->clientFactory = $clientFactory;
        $this->resultPageFactory = $pageFactory;
        parent::__construct($context);
    }

    public function execute()
    {

        $client = $this->clientFactory->create();
    $record = $client->city('128.101.101.101');
    print($record->country->isoCode . "\n"); 
    print($record->country->name . "\n"); 
    print($record->country->names['zh-CN'] . "\n");
    print($record->mostSpecificSubdivision->name . "\n");
    print($record->mostSpecificSubdivision->isoCode . "\n");
    print($record->city->name . "\n"); 
    print($record->postal->code . "\n"); 
    print($record->location->latitude . "\n"); 
    print($record->location->longitude . "\n");

    }

}

I even tried declaring API settings as below but it do not seem to work

$configs = array(
'userId' => 'MyUserID',
'licenseKey' => 'MylicesneKey',
'host' => 'geoip.maxmind.com'
);
$client = $this->clientFactory->create($configs);

In the file Tobai/GeoIp2/Model/WebService/ClientFactory.php, when I print $client, it returns nothing

public function create(array $data = [])
    {
        if (isset($data['options']) && is_string($data['options'])) {
            $data['options'] = ['host' => $data['options']];
        } elseif (empty($data['options'])) {
            $data['options'] = [];
        }

        $userId = !empty($data['userId']) ? $data['userId'] : $this->config->getUserId();
        $licenseKey = !empty($data['licenseKey']) ? $data['licenseKey'] : $this->config->getLicenseKey();
        $locales = !empty($data['locales']) ? $data['locales'] : ['en'];
        $options = $data['options'] + ['host' => $this->config->getHost()];

        $client = new \GeoIp2\WebService\Client($userId, $licenseKey, $locales, $options);
        return $client;
    }

On further inspection, it throws below exception

Fatal error: Uncaught Error: Class 'GeoIp2\WebService\Client' not found in /Tobai/GeoIp2/Model/WebService/ClientFactory.php:46

The document says

To use this API, you must create a new \GeoIp2\WebService\Client object with your $accountId and $licenseKey, then you call the method corresponding to a specific end point, passing it the IP address you want to look up.

How do I use GeoIP2 WebService example with above code? Am I missing out anything in terms of implementation?

Thanks



Related Questions


Magento2: Re-direct to custom URL from observer

Updated April 04, 2017 17:09 PM


Events and observers; what is being cached?

Updated February 28, 2017 17:09 PM