I am not a php expert. I develop android apps. In my app i am getting the user's ip address from this url http://ip2country.sourceforge.net/ip2c.php?format=JSON. As you can see when some open this url it returns some info including the IP address. I only want to get the IP and (if possible) country. Most of the time this url is busy and doesn't returns ip and gives max active user connections error. Can you please give me any php file which i can put in my own webhost and call the url to get ip. The data returned should be in json so i can parse it easily. Thanks
Asked
Active
Viewed 2,780 times
3 Answers
1
<?php
$json = file_get_contents("http://ip2country.sourceforge.net/ip2c.php?format=JSON");
//this $json will have the response that the website sends.
echo json_encode($json);
?>
You can have this object wherever you call this php file and do the needful
Run this php file to check the output
Another way: EDIT
<?php
$visitor_ip = $_SERVER['REMOTE_ADDR'];
echo $visitor_ip;
$data1 = file_get_contents("http://api.hostip.info/?ip=$visitor_ip");
echo "<br> $data1";
?>

Knight Rider
- 186
- 10
-
Ohh, I used it and it was showing result to me and btw Can you please give me any php file which i can put in my own webhost and call the url to get ip meant what then.?? – Knight Rider Jul 10 '14 at 16:03
-
can i get the ip if i replace file_get_contents("http://ip2country.sourceforge.net/ip2c.php?format=JSON"); with $_SERVER['REMOTE_ADDR']? – Taimur Ayaz Jul 10 '14 at 16:08
-
0
You get the IP address in PHP you have to use $_SERVER['REMOTE_ADDR']
then use http://ipinfo.io to pass that IP to this website. You can get the Location through JSON data, just follow the first answer on this question Getting the location from an IP address.
I have successfully implemented it by following the answer.
0
You can use PHP to get the users IP address via $_SERVER['REMOTE_ADDR']
. You can then use an IP to Location lookup website to translate that into a country and merge the results:
$ip = $_SERVER['REMOTE_ADDR'];
$loc = json_decode(file_get_contents('http://ipinfo.io/'.$ip.'/json'), true);
$country = isset($loc['country']) ? $loc['country'] : 'Unknown';
$result = array('ip'=>$ip, 'country'=>$country);
header('Content-Type: application/json');
echo json_encode($result);

cOle2
- 4,725
- 1
- 24
- 26