Without using a database of IP addresses or an "external API" (not completely true), this is the shortest method of determining the country of an IP addresss.
The one-liner issues a shell_exec() to the whois command and parses the two letter country code from it.
if(preg_match('/^(\d+\.?){4}$/',$ip)) {
$country = trim(shell_exec('whois '.$ip.' -H | grep country | awk \'{print $2}\''));
}
The preg_match() is of course optional, but for your own good, because as you may know, shell_exec() is VERY DANGEROUS.
Note: This does not work on all versions of WHOIS!
Also note that there is a daily query limit to the RIPE.NET database, so it's good to cache the results. It's not like that information gets changed often.