You have to first get the database of ip to country where as you can match the ip and retrieve the country to that ip.

MaxMind provides such data.