 <p>If you looking for some copy - paste solution, i foudn this code :</p> <pre><code> function countryCityFromIP($ipAddr) { //function to find country and city from IP address //Developed by Roshan Bhattarai http://roshanbh.com.np //verify the IP address for the ip2long($ipAddr)== -1 || ip2long($ipAddr) === false ? trigger_error("Invalid IP", E_USER_ERROR) : ""; $ipDetail=array(); //initialize a blank array //get the XML result from hostip.info $xml = file_get_contents("http://api.hostip.info/?ip=".$ipAddr); //get the city name inside the node &lt;gml:name&gt; and &lt;/gml:name&gt; preg_match("@&lt;Hostip&gt;(\s)*&lt;gml:name&gt;(.*?)&lt;/gml:name&gt;@si",$xml,$match); //assing the city name to the array $ipDetail['city']=$match[2]; //get the country name inside the node &lt;countryName&gt; and &lt;/countryName&gt; preg_match("@&lt;countryName&gt;(.*?)&lt;/countryName&gt;@si",$xml,$matches); //assign the country name to the $ipDetail array $ipDetail['country']=$matches[1]; //get the country name inside the node &lt;countryName&gt; and &lt;/countryName&gt; preg_match("@&lt;countryAbbrev&gt;(.*?)&lt;/countryAbbrev&gt;@si",$xml,$cc_match); $ipDetail['country_code']=$cc_match[1]; //assing the country code to array //return the array containing city, country and country code return $ipDetail; } </code></pre> <p>Hope this helps somebody.</p>