May
2
2009
2
2009
Get Country City from IP address in PHP
IPInfoDB provides a simple API to get the location of a visitor from his IP address. You need to first register and get an API Key.
Then this code can be used :
Code:
<?php
$url = "http://api.ipinfodb.com/v3/ip-city/?key=$your_key&ip=$ip&format=json";
$d = file_get_contents($url);
$data = json_decode($d , true);
/*
{
"statusCode" : "OK",
"statusMessage" : "",
"ipAddress" : "74.125.45.100",
"countryCode" : "US",
"countryName" : "UNITED STATES",
"regionName" : "CALIFORNIA",
"cityName" : "MOUNTAIN VIEW",
"zipCode" : "94043",
"latitude" : "37.3956",
"longitude" : "-122.076",
"timeZone" : "-08:00"
}
*/
if(strlen($data['countryCode']))
{
$info = array(
'ip' => $data['ipAddress'] ,
'country_code' => $data['countryCode'] ,
'country_name' => $data['countryName'] ,
'region_name' => $data['regionName'] ,
'city' => $data['cityName'] ,
'zip_code' => $data['zipCode'] ,
'latitude' => $data['latitude'] ,
'longitude' => $data['longitude'] ,
'time_zone' => $data['timeZone'] ,
);
}
return $info;
?>
IPinfodb supports json and xml formats for data output.
The above code can be seen in action at ipmango.com.
Another website GeoIO provides a similar API. You need to first register and get your code.
Then you can code like this :
$url = "http://api.geoio.com/q.php?key=$your_key&qt=geoip&d=comma&q=$ip";
$d = file_get_contents($url);
$data = explode(',' , $d);
/*
Kochi,Kerala,India,Nib (national Internet Backbone),9.9667,76.2333,IN
*/
$info = array(
'country_code' => $data[6] ,
'country_name' => $data[2] ,
'region_name' => $data[1] ,
'city' => $data[0] ,
'latitude' => $data[4] ,
'longitude' => $data[5] ,
'isp' => $data[3] ,
);
Geoio provides data in delimited format like comma delimited , pipe delimited and so on.
Popularity: 29% [?]
Tags: geolocation, php
Related Posts
Subscribe
Recent Posts
- Compile wxwebconnect on Ubuntu 11.04 64 bit
- Disqus Comments Importer Script in PHP
- Beginners’ guide to socket programming with winsock
- Handle multiple socket connections with fd_set and select on Linux
- Beginners guide to socket programming in C on Linux
- Gui whois client in python with wxpython
- Whois client code in C with Linux sockets
- str_replace for C
- Easy to use C/C++ IDE for Ubuntu Linux
- Get local ip in C on linux
Binarytides
Tags
apache
applications
box2d
bsnl
c
chrome
cron
css
database
dns
firefox
flash
freelance
game programming
gd
graphs
hacking
htaccess
html
html5
imagemagick
java
javascript
libpcap
linux
mod rewrite
moneybookers
mootools
mvc
mysql
networking
payment
paypal
php
phpmyadmin
python
ruby
security
Sockets
software
swing
ubuntu
winpcap
winsock
xdebug
An article by Binary Tides





Getting an Ip-Address of an city using the website It is possible in many websites.I have recently used the site http://www.ip-details.com.All the informations very fast,free of cost also..
pls provide me code for get location of state and coutry in cakephp