|
 |

Examples below show how to use the MaxMind City web service API from server-side scripts in Perl, ASP, PHP, ColdFusion and C#.
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;
use Text::CSV;
# replace this value with license key
my $license_key = "LICENSE_KEY_HERE";
my $ua = LWP::UserAgent->new(timeout => 2);
my $h = HTTP::Headers->new;
my $csv_parser = new Text::CSV;
$h->content_type('application/x-www-form-urlencoded');
my $request = HTTP::Request->new('POST','http://geoip1.maxmind.com/f',
$h,"l=$license_key&i=24.24.24.24");
my $res = $ua->request($request);
my $content = $res->content;
print "content = $content\n";
$csv_parser->parse($content);
my ($country, $region, $city, $postal, $lat, $lon, $metro_code,
$area_code, $isp, $org, $err) = $csv_parser->fields();
print "location = $country, $region, $city, $lat, $lon\n";
|  |
Active Server Pages (ASP) Example
Dim objHttp, strQuery
strQuery = "http://geoip1.maxmind.com/f?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.
VBScript Example
Set request = Server.CreateObject("AspHTTP.Conn")
request.Url = "http://geoip1.maxmind.com/f?l=" & license_key & "&i=" & ip_address
request.RequestMethod = "GET"
string = request.GetURL
data = Split(string, ",")
country = arr(0)
region = arr(1)
city = arr(2)
postal = arr(3)
latitude = arr(4)
longitude = arr(5)
metro_code = arr(6)
area_code = arr(7)
isp = arr(8)
organization = arr(9)
error = arr(10)
|  |
PHP Example
$query = "http://geoip1.maxmind.com/f?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;
|  |
Nathan White has contributed a PHP API that has better
handling for parsing commas, available here.
ColdFusion Example
ColdFusion GeoIP City/ISP/Org web service example
VB.NET Example
VB.NET GeoIP City/ISP/Org web service example
SQLCLR Example
VB.NET SQLCLR GeoIP City web service example, from
Tim Shay's Blog
C# Example
// Contributed by Gokhan Saltik
private string ReturnData(string IP)
{
System.Uri objUrl = new System.Uri("http://geoip1.maxmind.com/b?l=KEYKEYKEY&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;
}
|  |
|