themban
themban

Reputation: 17

Using maxmid GeoIP2 PHP API with CodeIgniter

I am trying to use GeoIP2 PHP API ( https://github.com/maxmind/GeoIP2-php ) within CodeIgniter. How can I load GeoIP2 and use it for user geolocation?

I have tried loading it like this:

$this->load->library("GeoIp2/Database/Reader");

or

    require APPPATH . "libraries/GeoIp2/ProviderInterface.php";
    require APPPATH . "libraries/GeoIp2/Database/Reader.php";

or

$this->load->file("GeoIp2/ProviderInterface");
$this->load->library("GeoIp2/Database/Reader");

I get this error: "Unable to load the requested file: ProviderInterface"

I have looked this Namespace in PHP CodeIgniter Framework , but i have little experience with namespaces.

No success with this, I am not winning, I really do not know how to implement this within CodeIgniter.

Upvotes: 1

Views: 1997

Answers (3)

munjal
munjal

Reputation: 1404

I was trying to find the solution of this question. But couldn't find on stackoverflow. I am writing my own code here. Maybe, it will be helpful for someone. I have added a new function in my utility_helper.php file :

function get_ip_country_code($ip_address) {

  require APPPATH .'third_party/GeoIP2/autoload.php';
  $reader = new GeoIp2\Database\Reader(FCPATH.'public/geoip/GeoIP2-Country.mmdb');

  $record = $reader->country($ip_address);

  return $record->country->isoCode;
}

I put the GeoIP2 library in the third_party folder and put the mmdb file in the public folder. It works fine for me. I hope it will save someone's time :)

Upvotes: 1

tmarois
tmarois

Reputation: 2470

Few ways you can embed this within CodeIgniter.

First, you need to include it within the script:

require_once( 'GeoIp2/vendor/autoload.php' );
use GeoIp2\Database\Reader;

Next, I call Reader() for the detection methods

$reader = new Reader('GeoIp2/GeoIP2-City.mmdb');
$record = $reader->city($ip);

// Country (code)
$record->country->isoCode; 

// State
$record->mostSpecificSubdivision->name; 

// City
$record->city->name; 

// zip code
$record->postal->code; 

I just tested this on CodeIgniter 3x and works.

I used a bridge class. Inside /application/libraries create a file called CI_GeoIp2.php and add the following code.

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * GeoIp2 Class
 *
 * @package       CodeIgniter
 * @subpackage  Libraries
 * @category      GeoIp2
 * @author        Timothy Marois <[email protected]>
 */
require_once( APPPATH . 'third_party/GeoIp2/vendor/autoload.php' );
use GeoIp2\Database\Reader;

class CI_GeoIp2 { 

    protected $record;
    protected $database_path = 'third_party/GeoIp2/GeoIP2-City.mmdb';

    public function __construct() {

        $ci =& get_instance();
        $reader = new Reader(APPPATH.$this->database_path);

        $ip = $ci->input->ip_address();
        if ($ci->input->valid_ip($ip)) {
            $this->record = $reader->city($ip);
        }

        log_message('debug', "CI_GeoIp2 Class Initialized");
    }


    /**
     * getState()
     * @return state
     */
    public function getState() {
        return $this->record->mostSpecificSubdivision->name;;
    }


    /**
     * getState()
     * @return country code "US/CA etc"
     */
    public function getCountryCode() {
        return $this->record->country->isoCode;
    }


    /**
     * getCity()
     * @return city name
     */
    public function getCity() {
        return $this->record->city->name;
    }


    /**
     * getZipCode()
     * @return Zip Code (#)
     */
    public function getZipCode() {
        return $this->record->postal->code;
    }


    /**
     * getRawRecord()
     * (if you want to manually extract objects)
     *
     * @return object of all items
     */
    public function getRawRecord() {
        return $this->record;
    }

}

Now you can either autoload or load it up using

 $this->load->library("CI_GeoIp2");

I prefer to autoload it like this under autoload.php config

 $autoload['libraries'] = array('CI_GeoIp2'=>'Location');

So within the script I use,

$this->Location->getState() 
$this->Location->getCity() 

... and so on

Upvotes: 0

cruzzzin
cruzzzin

Reputation: 33

The GeoIp2 php sdk takes advantage of PHP's namespace feature, which the CodeIgniter framework does not support, which is why you're getting the error when you try to load the library. The post you linked to offers a solution using spl_autoload, however I do not use CodeIgniter and haven't tested it with the GeopIp2 php sdk.

Upvotes: 0

Related Questions