MaxMind
Support My Account FAQ My Order
Support My Account FAQ My Order

Country Usage
City Usage
City/ISP/Org Usage
Error Codes
Support Center


City Web Service: Usage from Perl, ASP and PHP

Examples below show how to use the MaxMind City web service API from server-side scripts in Perl, ASP and PHP. Any programming language that supports HTTP client calls should be able to use MaxMind web services.

Perl Example
#!/usr/bin/perl

use strict;
use LWP::UserAgent;
use HTTP::Request qw(GET POST);
use HTTP::Headers;

# replace this value with license key
my $license_key = "LICENSE_KEY_HERE";

my $ua = LWP::UserAgent->new(timeout => 2);
my $h = HTTP::Headers->new;
$h->content_type('application/x-www-form-urlencoded');
my $request = HTTP::Request->new('POST','http://geoip3.maxmind.com/b',
				$h,"l=$license_key&i=24.24.24.24");
my $res = $ua->request($request);
my $content = $res->content;
print "content = $content\n";
my ($country, $region, $city, $lat, $lon) = split(",",$content);
print "location = $country, $region, $city, $lat, $lon\n";
Active Server Pages (ASP) Example
Dim objHttp, strQuery
strQuery = "http://geoip3.maxmind.com/b?l=" & license_key & _
	"&i=" & ipaddress
set objHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
objHttp.open "GET", strQuery, false
objHttp.send
Response.Write objHttp.ResponseText
Set objHttp = Nothing
Requirements:

  1. ASP 3.0+
  2. Microsoft® XML 3.0 Component
Microsoft XML 3.0 Component can be downloaded for free here.

PHP Example
$query = "http://geoip3.maxmind.com/b?l=" . $license_key . "&i=" . $ipaddress;
$url = parse_url($query);
$host = $url["host"];
$path = $url["path"] . "?" . $url["query"];
$timeout = 1;
$fp = fsockopen ($host, 80, $errno, $errstr, $timeout)
	or die('Can not open connection to server.');
if ($fp) {
  fputs ($fp, "GET $path HTTP/1.0\nHost: " . $host . "\n\n");
  while (!feof($fp)) {
    $buf .= fgets($fp, 128);
  }
  $lines = split("\n", $buf);
  $data = $lines[count($lines)-1];
  fclose($fp);
} else {
  # enter error handing code here
}
echo $data;
$geo = explode(",",$data);
$country = $geo[0];
$state = $geo[1];
$city = $geo[2];
$lat = $geo[3];
$lon = $geo[4];


MaxMind, GeoIP and related marks are registered trademarks of MaxMind, Inc.
Copyright © 2008 MaxMind, Inc. All Rights Reserved. Terms of use.