5

I downloaded and extracted GeoLite2-City.mmdb, GeoLite2-Country.mmdb. Both are in htdocs\geoip\

Then, I ran this script. Trouble is, how does this thing work? What is require_once 'vendor/autoload.php'; supposed to contain? Am I missing anything here. I used to use the older versions of these that came as .dat file, and had no problem with them. These .mmdb ones are a little hard for me to crack. All I'm trying to do is get the Country Code, Country Name and other data to store in a db when a user uses the search tool on a page. How do I get this going?

My test page taken from the site

<?php
require_once 'vendor/autoload.php'; //What is this supposed to contain?
use GeoIp2\Database\Reader; // What is this too?

// This creates the Reader object, which should be reused across
// lookups.
$reader = new Reader($_SERVER['DOCUMENT_ROOT'].'\geoip\GeoIP2-City.mmdb'); // Where my DB resides

// Replace "city" with the appropriate method for your database, e.g.,
// "country".
$record = $reader->city('128.101.101.101');

print($record->country->isoCode . "\n"); // 'US'
print($record->country->name . "\n"); // 'United States'
print($record->country->names['zh-CN'] . "\n"); // '??'

print($record->mostSpecificSubdivision->name . "\n"); // 'Minnesota'
print($record->mostSpecificSubdivision->isoCode . "\n"); // 'MN'

print($record->city->name . "\n"); // 'Minneapolis'

print($record->postal->code . "\n"); // '55455'

print($record->location->latitude . "\n"); // 44.9733
print($record->location->longitude . "\n"); // -93.2323

?>
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
Norman
  • 6,159
  • 23
  • 88
  • 141

2 Answers2

2

I would recommend using the PHP Extension API if you are concerned at all about performance. You can get upwards of 7 million queries per second with the PHP (C API) extension vs 9,000 qps with the pure PHP API.

I describe how to compile the extension, and how to use the mmdb databases in PHP here:

Intro to Maxmind GeoLite2 with Kohana PHP

Geolocate IP Address

Jamie
  • 811
  • 9
  • 13
1

This package is supposed to be installed through Composer. When you have composer installed and your project is ready in your composer.json should appear a new record:

    {
    "require": {
        "geoip2/geoip2": "0.5.*"
    }

You can find more information about how to install that package in this url: https://github.com/maxmind/GeoIP2-php

Follow each step in order to install Composer and get all dependencies correctly.

ssola
  • 368
  • 1
  • 9
  • Ok, but can I install / do all this if I'm on a shared hosting plan? Right now I'm just demoing things and have purchased shared hosting on GoDaddy. Can I do this there or can I do this only If I have my own server and access to root etc. etc. (Not very familiar with that stuff) – Norman Dec 18 '13 at 08:45
  • Yes sure, you can use composer in your local machine and upload all the content to your FTP. So, is not necessary to use Composer directly in the server. – ssola Dec 18 '13 at 08:52
  • Actually, I meant to ask if I can use this when I'm using a shared hosting plan like GoDaddy's. Not the local machine. – Norman Dec 18 '13 at 09:02
  • Yes you can use it. But you have to use Composer in your local machine in order to generate all the necessary files like autoload.php (this one is generated by Composer). When you have all the files then you can transfer them with your preferred FTP account. – ssola Dec 18 '13 at 09:03
  • Ok. Thanks. You explained that very well. – Norman Dec 18 '13 at 09:05
  • I seem to be running into all sort of problems getting this thing to run on Windows. If you have the files needed to get this thing running, can you share them? – Norman Dec 18 '13 at 11:46
  • It's a lengthy process. I could send you all the files instead. Simply unzip to htdoc and run. – Norman Jan 26 '14 at 16:53