ytorbyk / magento2-geo-ip2

Magento 2 extension: Integrates MaxMind GeoIP2 DB into Magento 2.
MIT License
17 stars 7 forks source link

How to use this extension with GeoIP2 WebService? #6

Open Vikram0811 opened 6 years ago

Vikram0811 commented 6 years ago

Hi,

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

I am referring your WebService example in User Guide. 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

Vikram0811 commented 6 years ago

Hi

Any updates on this?