Discussion:
Grabbing IP address information
Jennifer
2014-01-14 00:54:50 UTC
Permalink
Hi everyone!

My name's Jenni and I'm new to the list. I hope this question isn't too simple.

I want to grab some information about an IP address and present it in a readable fashion, so I wrote the following:

$location = file_get_contents('http://freegeoip.net/json/78.18.200.182');
$location = str_replace('{', '', $location);
$location = str_replace('}', '', $location);
$location = str_replace('"', '', $location);
$location = str_replace(',', "\n", $location);
echo $location;

However, this seems longer than it needs to be. Is there a better way to do this?

Thank you,
Jenni

Superior Shelving Systems::::....
http://www.SuperiorShelving.com

The (Storage|Office|Display) Shelving Specialists
Since 1984

Computer Workstations:
http://www.superiorshelving.com/mfg/nexel/pages/lan-workstations-nexel.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Aziz Saleh
2014-01-14 00:58:54 UTC
Permalink
$location = file_get_contents('http://freegeoip.net/json/78.18.200.182');
$location = json_decode($location);
print_r($location);

Alternatively, you can use maxmind geoip library (they have a free database
out there):

http://www.php.net/manual/en/book.geoip.php
http://dev.maxmind.com/geoip/legacy/geolite/
Post by Jennifer
Hi everyone!
My name's Jenni and I'm new to the list. I hope this question isn't too simple.
I want to grab some information about an IP address and present it
$location = file_get_contents('http://freegeoip.net/json/78.18.200.182');
$location = str_replace('{', '', $location);
$location = str_replace('}', '', $location);
$location = str_replace('"', '', $location);
$location = str_replace(',', "\n", $location);
echo $location;
However, this seems longer than it needs to be. Is there a better way to do this?
Thank you,
Jenni
Superior Shelving Systems::::....
http://www.SuperiorShelving.com
The (Storage|Office|Display) Shelving Specialists
Since 1984
http://www.superiorshelving.com/mfg/nexel/pages/lan-workstations-nexel.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Xinhao Zheng
2014-01-14 01:02:33 UTC
Permalink
hi,
the response is in json format,you can call json_decode to convert the
response string to a php object,ie

$ip_obj = json_decode($location);
echo $ip_obj->ip . $ip_obj->country_name;

----------
george
Post by Jennifer
Hi everyone!
My name's Jenni and I'm new to the list. I hope this question isn't too simple.
I want to grab some information about an IP address and present it
$location = file_get_contents('http://freegeoip.net/json/78.18.200.182');
$location = str_replace('{', '', $location);
$location = str_replace('}', '', $location);
$location = str_replace('"', '', $location);
$location = str_replace(',', "\n", $location);
echo $location;
However, this seems longer than it needs to be. Is there a better way to do this?
Thank you,
Jenni
Superior Shelving Systems::::....
http://www.SuperiorShelving.com
The (Storage|Office|Display) Shelving Specialists
Since 1984
http://www.superiorshelving.com/mfg/nexel/pages/lan-workstations-nexel.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jennifer
2014-01-14 17:48:43 UTC
Permalink
the response is in json format,you can call json_decode to convert the response string to a php object
This is perfect, George. Thank you so much!

I also want to thank Aziz for his help.

Jenni

Superior Shelving Systems::::....
http://www.SuperiorShelving.com

The (Storage|Office|Display) Shelving Specialists
Since 1984

Metro Shelving:
http://www.superiorshelving.com/mfg/metro/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Continue reading on narkive:
Loading...