0

I am trying to use http://www.hostip.info/use.html in my web app to show the approximate City location of an IP address. I cannot figure out how to actually show the contents of the API... Here is what I have that is not working.

function showCity($currIP){

$lookupData = 'http://api.hostip.info/get_html.php?ip='.$currIP;
return $lookupData;

}

2 Answers 2

2

Your API returns this:

Country: UNITED STATES (US)
City: Seattle, WA
IP: 168.111.127.225

So you need to do some string parsing on that result. Using the below will get your started:

 $array = preg_split('/$\R?^:/m', $lookupData);
 print_r($array);

Try this instead:

$array = preg_split("/[\r\n]/", $lookupData, -1, PREG_SPLIT_NO_EMPTY);

Also, as was mentioned by mcmajkel, if you use the JSON api link, you can get to it with this:

$lookupData = 'http://api.hostip.info/get_json.php?ip='.$currIP;
$api = json_decode($lookupData);
$myName = $api->country_name;
$myCode = $api->country_code;
$myCity = $api->city;
$myIP   = $api->ip;
Sign up to request clarification or add additional context in comments.

2 Comments

This does not seem to work. I get this: Array ( [0] => api.hostip.info/get_html.php?ip=xxx.xxx.xxx.xxx )
Adjusted answer with new regex - give it a try
1

This call returns string, as mentioned by GregP. But you can call http://api.hostip.info/get_json.php?ip=12.215.42.19

And get a nice piece of JSON in return, which will be easier to parse

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.