A little script I wrote in PHP for GeoIP lookups


Being fully transparent I am a system admin by trade so my code is not the greatest. That being said, I figured I would release a little bit of PHP code I wrote for a tool I have created as it is useful for pulling GeoIP data and displaying it in HTML.  It uses PHP Curl, gethostbyname, JSON decode, and the Telize.com JSON GeoIP webservice.


// PHP GeoIP Lookup Script - 11/15/2014 by Dustin Larmeir (dustin@larmeir.com)
// Form Input

// Grabs the domain name via the input form and converts it to a IP address
$hostname = $_POST['INPUT'];
$ipaddress = gethostbyname($hostname);

// Grabs IP address from form (basic method)
$url = "http://www.telize.com/geoip/".$ipaddress;
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
// Closing

// get the results and json_decode the answer
$geoip_data = (json_decode($geoip_json, true));

// Echo output from JSON decoding and PHP arrays
echo "IP Address: "; echo($geoip_data["ip"]);
echo "Organization: "; echo($geoip_data["isp"]);
echo "City: "; echo($geoip_data[city]);
echo "State: "; echo($geoip_data[region]);
echo "Country: "; echo($geoip_data[country]);
echo "Time Zone: "; echo($geoip_data[timezone]);
echo "";