|
 |

Examples below show how to use the MaxMind Country web service API from server-side scripts in Perl, ASP and PHP.
These can be adapted for other web services.
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;
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://geoip.maxmind.com/a',
$h,"l=$license_key&i=80.24.24.24");
my $res = $ua->request($request);
my $content = $res->content;
print "content = $content\n"
|  |
Active Server Pages (ASP) Example
Dim objHttp, strQuery
strQuery = "http://geoip.maxmind.com/a?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:
- ASP 3.0+
- Microsoft® XML 3.0 Component
Microsoft XML 3.0 Component can be downloaded for free from
here.
PHP Example
$query = "http://geoip.maxmind.com/a?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);
if ($fp) {
fputs ($fp, "GET $path HTTP/1.0\nHost: " . $host . "\n\n");
while (!feof($fp)) {
$buf .= fgets($fp, 128);
}
$lines = explode("\n", $buf);
$country = $lines[count($lines)-1];
fclose($fp);
} else {
# enter error handing code here
}
echo $country;
|  |
Python Example
import urllib2
license_key = 'LICENSE_KEY_HERE'
ip_address = '80.24.24.24'
url = 'http://geoip.maxmind.com/a?l=' + license_key + '&i=' + ip_address
try:
result = urllib2.urlopen(url)
print result.read()
except urllib.URLError, e:
print e
|  |
Java Example
import java.net.MalformedURLException;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Country {
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/a?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) {
// Do something with output
System.out.println(inLine);
}
in.close();
}
}
|  |
C# Example
private string ReturnData(string ip)
{
string license_key = "LICENSE_KEY_HERE";
System.Uri objUrl = new System.Uri("http://geoip.maxmind.com/a?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;
}
|  |
ColdFusion Example
ColdFusion GeoIP Country web service example
|