It's not perfect, but it seems to be good enough for my purposes.
$ip = $_SERVER['REMOTE_ADDR'];
$ua = $_SERVER['HTTP_USER_AGENT'];
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "freegeoip.net/csv/" . $ip);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$ip = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$array = explode(',', $ip);
Re:Show HN: IP Lookup
by fiorix on Tuesday November 19, 2013 @09:01AM
(#6761162)
Author of freegeoip.net here. It uses the db from MaxMind.
Worth mentioning that the system is not only free but also open source, available at github.com/fiorix/freegeoip (currently written in Go)
It's not perfect, but it seems to be good enough for my purposes.