|
 |

Examples below show how to use the MaxMind Omni web service API from server-side scripts in Perl, ASP, VBScript, PHP, and C#.
Any programming language that supports HTTP client calls should be able to use MaxMind web services.
See the full list of data fields returned.
Perl Example
#!/usr/bin/perl
use strict;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Headers;
use Text::CSV_XS;
# replace this value with license key
my $license_key = "LICENSE_KEY_HERE";
my $csv = Text::CSV_XS->new( { binary => 1 } );
my $ua = LWP::UserAgent->new( timeout => 5 );
my $h = HTTP::Headers->new;
$h->content_type('application/x-www-form-urlencoded');
my $request = HTTP::Request->new( 'POST', 'http://geoip.maxmind.com/e',
$h, "l=$license_key&i=24.24.24.24" );
my $res = $ua->request($request);
my $content = $res->content;
print "content = $content\n";
$csv->parse($content);
my (
$countrycode, $countryname, $regioncode, $regionname,
$city, $lat, $lon, $metrocode,
$areacode, $timezone, $continent, $postalcode,
$isp, $org, $domain, $asnum,
$netspeed, $usertype, $accuracyradius, $countryconf,
$cityconf, $regionconf, $postalconf, $err
) = $csv->fields;
print "results = $countrycode, $countryname, $regioncode, $regionname,
$city, $lat, $lon, $metrocode, $areacode, $timezone, $continent,
$postalcode, $isp, $org, $domain, $asnum, $netspeed, $usertype, $accuracyradius,
$countryconf, $cityconf, $regionconf, $postalconf, $err\n";
|  |
Python Example
import urllib2
import csv
license_key = 'LICENSE_KEY_HERE'
ip_address = '80.24.24.24'
url = 'http://geoip.maxmind.com/e?l=' + license_key + '&i=' + ip_address
try:
result = urllib2.urlopen(url)
except urllib.URLError, e:
print e
output = result.read()
reader = csv.reader([output])
countrycode, countryname, regioncode, regionname, city, lat, lon, \
metrocode, areacode, timezone, continent, postalcode, isp, organization, \
domain, asnum, netspeed, usertype, accuracyradius, countryconf, cityconf, \
regionconf, postalconf, error = reader.next()
|  |
Java Example
import java.net.MalformedURLException;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class OmniReader {
public static void main(String[] args) throws Exception {
String license_key = "LICENSE_KEY_HERE";
String ip_address = "80.24.24.24";
String url_str = "http://geoip.maxmind.com/e?l=" + license_key + "&i=" + ip_address;
URL url = new URL(url_str);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String inLine;
while ((inLine = in.readLine()) != null) {
// Alternatively use a CSV parser here.
Pattern p = Pattern.compile("\"([^\"]*)\"|(?<=,|^)([^,]*)(?:,|$)");
Matcher m = p.matcher(inLine);
ArrayList fields = new ArrayList();
String f;
while (m.find()) {
f = m.group(1);
if (f!=null) {
fields.add(f);
}
else {
fields.add(m.group(2));
}
}
// ... use fields
String countrycode = fields.get(0);
String countryname = fields.get(1);
String regioncode = fields.get(2);
String regionname = fields.get(3);
String city = fields.get(4);
String lat = fields.get(5);
String lon = fields.get(6);
String metrocode = fields.get(7);
String areacode = fields.get(8);
String timezone = fields.get(9);
String continent = fields.get(10);
String postalcode = fields.get(11);
String isp = fields.get(12);
String org = fields.get(13);
String domain = fields.get(14);
String asnum = fields.get(15);
String netspeed = fields.get(16);
String usertype = fields.get(17);
String accuracyradius = fields.get(18);
String countryconf = fields.get(19);
String cityconf = fields.get(20);
String regionconf = fields.get(21);
String postalconf = fields.get(22);
String error = fields.get(23);
}
in.close();
}
}
|  |
Active Server Pages (ASP) Example
Set request = Server.CreateObject("AspHTTP.Conn")
request.Url = "http://geoip.maxmind.com/e?l=" & license_key & "&i=" & ip_address
request.RequestMethod = "GET"
string = request.GetURL
data = Split(string, ",")
countrycode = data(0)
countryname = data(1)
regioncode = data(2)
regionname = data(3)
city = data(4)
lat = data(5)
lon = data(6)
metrocode = data(7)
areacode = data(8)
timezone = data(9)
continent = data(10)
postalcode = data(11)
isp = data(12)
organization = data(13)
domain = data(14)
asnum = data(15)
netspeed = data(16)
usertype = data(17)
accuracyradius = data(18)
countryconf = data(19)
cityconf = data(20)
regionconf = data(21)
postalconf = data(22)
error = data(23)
|  |
Requirements:
- ASP 3.0+
- Microsoft® XML 3.0 Component
Microsoft XML 3.0 Component can be downloaded for free from
here.
VBScript Example
Set request = Server.CreateObject("AspHTTP.Conn")
request.Url = "http://geoip.maxmind.com/e?l=" & license_key & "&i=" & ip_address
request.RequestMethod = "GET"
string = request.GetURL
data = Split(string, ",")
countrycode = data(0)
countryname = data(1)
regioncode = data(2)
regionname = data(3)
city = data(4)
lat = data(5)
lon = data(6)
metrocode = data(7)
areacode = data(8)
timezone = data(9)
continent = data(10)
postalcode = data(11)
isp = data(12)
organization = data(13)
domain = data(14)
asnum = data(15)
netspeed = data(16)
usertype = data(17)
accuracyradius = data(18)
countryconf = data(19)
cityconf = data(20)
regionconf = data(21)
postalconf = data(22)
error = data(23)
|  |
PHP Example
$query = "http://geoip.maxmind.com/e?l=" . $license_key . "&i=" . $ipaddress;
$url = parse_url($query);
$host = $url["host"];
$path = $url["path"] . "?" . $url["query"];
$timeout = 2;
$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 = explode("\n", $buf);
$data = $lines[count($lines)-1];
fclose($fp);
} else {
# enter error handing code here
}
echo $data;
$geo = str_getcsv($data);
$countrycode = $geo[0];
$countryname = $geo[1];
$regioncode = $geo[2];
$regionname = $geo[3];
$city = $geo[4];
$lat = $geo[5];
$lon = $geo[6];
$metrocode = $geo[7];
$areacode = $geo[8];
$timezone = $geo[9];
$continent = $geo[10];
$postalcode = $geo[11];
$isp = $geo[12];
$org = $geo[13];
$domain = $geo[14];
$asnum = $geo[15];
$netspeed = $geo[16];
$usertype = $geo[17];
$accuracyradius = $geo[18];
$countryconf = $geo[19];
$cityconf = $geo[20];
$regionconf = $geo[21];
$postalconf = $geo[22];
$error = $geo[23];
|  |
PHP Example ( PHP4 )
$query = "http://geoip.maxmind.com/e?l=" . $license_key . "&i=" . $ipaddress;
$url = parse_url($query);
$host = $url["host"];
$path = $url["path"] . "?" . $url["query"];
$timeout = 2;
$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 = explode("\n", $buf);
$data = $lines[count($lines)-1];
fclose($fp);
} else {
# enter error handing code here
}
echo $data;
preg_match_all('/(?:\A|,)("[^"]*"|[^,]*)/', $data, $out) ;
$geo = $out[1];
$countrycode = $geo[0];
$countryname = $geo[1];
$regioncode = $geo[2];
$regionname = $geo[3];
$city = $geo[4];
$lat = $geo[5];
$lon = $geo[6];
$metrocode = $geo[7];
$areacode = $geo[8];
$timezone = $geo[9];
$continent = $geo[10];
$postalcode = $geo[11];
$isp = $geo[12];
$org = $geo[13];
$domain = $geo[14];
$asnum = $geo[15];
$netspeed = $geo[16];
$usertype = $geo[17];
$accuracyradius = $geo[18];
$countryconf = $geo[19];
$cityconf = $geo[20];
$regionconf = $geo[21];
$postalconf = $geo[22];
$error = $geo[23];
|  |
C# Example
// Contributed by Gokhan Saltik
private string ReturnData(string IP)
{
System.Uri objUrl = new System.Uri("http://geoip.maxmind.com/e?l=LICENSE_KEY&i=" + IP);
System.Net.WebRequest objWebReq;
System.Net.WebResponse objResp;
System.IO.StreamReader sReader;
string strReturn=string.Empty;
//Try to connect to the server and retrieve data.
try
{
objWebReq = System.Net.WebRequest.Create(objUrl);
objResp = objWebReq.GetResponse();
//Get the data and store in a return string.
sReader = new System.IO.StreamReader(objResp.GetResponseStream());
strReturn = sReader.ReadToEnd();
//Close the objects.
sReader.Close();
objResp.Close();
}
catch (Exception ex)
{
}
finally
{
objWebReq = null;
}
return strReturn;
}
|  |
|