Which one looks the most trustworthy is up to you :)
Otherwise, there are scripts which are based on local databases on your server. The database data needs to be updated regularly, though. Check out this one:
Edit:
And of course, depending on your project you might want to look at HTML5 Location features. You can't use them yet on the Internet Explorer (IE9 will support it, long way to go yet), but in case your audience is mainly on mobile devices or using Safari/Firefox it's definitely worth to look at it!
Once you have the coordinates, you can reverse geocode them to a country code. Again there are APIs like this one:
Update, April 2013
Today I would recommend using Geocoder, a PHP library which makes it very easy to geocode ip addresses as well as postal address data.
***Update, September 2016
Since Google's privacy politics has changed, you can't use HTML5 Geolocation API if your server doesn't have HTPPS certificate or user doesn't allow you check his location. So now you can use user's IP and check in in PHP or get HTTPS certificate.
Import the CSV to your database (make sure to create an index for ip_long_from + ip_long_to). Then you can simply do:
$iplong = ip2long($_SERVER['REMOTE_ADDR']);
// should use mysqli with prepared statements etc, but just an example
mysql_query("
SELECT country
FROM ip_table
WHERE $iplong BETWEEN ip_long_from AND ip_long_to
LIMIT 1
");
Web APIs are a nice quick and easy solution, but if you need to do a lot of lookups then having an IP -> country database on your own machine is a better solution. MaxMind offer a free database that you can use with various PHP libraries, including GeoIP.
Basically you need to install the IP database table and then do mysql query for the IP for location.
<?php
include 'config.php';
mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) OR die(mysql_error());
mysql_select_db(DB_NAME) OR die(mysql_error());
$ip = $_SERVER['REMOTE_ADDR'];
$long = sprintf('%u', ip2long($ip));
$sql = "SELECT * FROM `geo_ips` WHERE '$long' BETWEEN `ip_start` AND `ip_end`";
$result = mysql_query($sql) OR die(mysql_error());
$ip_detail = mysql_fetch_assoc($result);
if($ip_detail){
echo $ip_detail['country']."', '".$ip_detail['state']."', '".$ip_detail['city'];
}
else{
//Something wrong with IP
}
If you need to have a good and updated database, for having more performance and not requesting external services from your website, here there is a good place to download updated and accurate databases.
I'm recommending this because in my experience it was working excellent even including city and country location for my IP which most of other databases/apis failed to include/report my location except this service which directed me to this database. (My ip location was from "Rasht" City in "Iran" when I was testing and the ip was: 2.187.21.235 equal to 45815275)
Therefore I recommend to use these services if you're unsure about which service is more accurate:
You're allowed up to 15,000 queries per hour. I think that's enough in most of the cases. Otherwise, you should think about using local databases from MaxMind GeoIP.
Freegeoip is open-source and you can find sources on github, deploy it locally and get rid of the query limits.
Also, service provides latitude/longitude/timezone and other useful stuff.