0

http://api.hostip.info/?ip=87.14.94.152

From this link (xml) i am tried to retrive countryName and countryAbbrev like this:

$url = 'http://api.hostip.info/?ip=87.14.94.152';
$xml = simplexml_load_file($url) or die("feed not loading");
$Country = $xml->gml['featureMember']->Hostip->countryName;
echo $Country;
echo 'BREAK HTML';
echo "-----";
echo "// "; var_dump($xml); echo " //";

but $Country is blank, any idea about?

thanks in advance

2
  • You should add the result of your var_dump($xml) to the question. Commented Dec 13, 2013 at 20:56
  • BREAK HTML-----// object(SimpleXMLElement)#1 (1) { ["@attributes"]=> array(1) { ["version"]=> string(5) "1.0.1" } } // Commented Dec 13, 2013 at 20:57

6 Answers 6

1

Try this not like an answer, but, just another way to do the same:

$data = file_get_contents('http://api.hostip.info/get_html.php?ip=87.14.94.152&position=true');

$arrayofdata = explode("\n", $data);
$country = explode(":", $arrayofdata[0]);
$count = explode(" ", $country[1]);

echo "Country Name: ".$count[1]."</br>";    //Prints ITALY
echo "Country Abbv: ".trim($count[2],"()"); //Prints IT

The position=true url part, it's just in case that you want to retrieve the coordinates.

Cheers ;)

Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

$url = 'http://api.hostip.info/?ip=87.14.94.152';
$xml = simplexml_load_file($url) or die("feed not loading");

$fm=$xml->xpath('//gml:featureMember');
print_r($fm[0]->Hostip->countryName);

Comments

1

Make a local copy of the xml and it will work. Just tested this and got the data back:

$url = 'http://api.hostip.info/?ip=87.14.94.152';
$data = file_get_contents($url);
$xml = simplexml_load_file($data) or die("feed not loading");

Comments

1

The countryName node is nested in a deeper level. You can use the children() method to access attributes with colon. Here's how you can get the country name:

$countryName = (string) $xml->children('gml', true)
                            ->featureMember->children('', true)
                                ->Hostip->countryName; // => ITALY

You could also use an XPath expression to retrieve the country name. This is easier:

$hostip = $xml->xpath('//Hostip'); 
$countryName = $hostip[0]->countryName;       // => ITALY
$countryAbbrev = $hostip[0]->countryAbbrev;   // => IT

1 Comment

Hi tenks in advance but $countryAbbrev return [object Object]
1
     protected function getCountryNameFromIP()
     {

        $ip  = $_SERVER['REMOTE_ADDR'];
        $opts = array(
        'http'=>array(
        'method'=>"GET",
        'header'=>"Accept-language: en\r\n" .
                  "Cookie: foo=bar\r\n"
           )
        );

        $context = stream_context_create($opts);
        $answerIP = @file_get_contents("http://api.ipinfodb.com/v3/ip-country/?key=4b585e37503a519a408dc17878e6ec04fa963e1b946c567722538d9431c2d5cb&format=xml&ip=$ip" ,false,$context);

        if(isset($answerIP) && $answerIP !="")
        {
            $theResJ            =   simplexml_load_string($answerIP);
            $last_login_ip_cn   =   $theResJ->countryName;


            /**
            *   $last_login_ip_cc   =   $theResJ->countryCode;
            *   $last_login_ip_rc   =   $theResJ->regionCode;
            *   $last_login_ip_rn   =   $theResJ->regionName;
            *   $last_login_ip_cp   =   $theResJ->cityName;
            *   $last_login_ip_lat  =   $theResJ->latitude;
            *   $last_login_ip_lng  =   $theResJ->longitude;
            *   $last_login_zip_code=   $theResJ->zipCode;
            */
        }
        else
        {
            $last_login_ip_cn   =   "";
            /**
            *   $last_login_ip_cc   =   "";
            *   $last_login_ip_rc   =   "";
            *   $last_login_ip_rn   =   "";
            *   $last_login_ip_cp   =   "";
            * $last_login_ip_lat    =   "";
            *   $last_login_ip_lng  =   "";
            *   $last_login_zip_code=   "";
            */
        }

        return $last_login_ip_cn;
       }

I hope it helps you

Comments

1

I agree. I just tried both answers and got good results. Here is the test code I just ran:

<?php
$url = 'http://api.hostip.info/?ip=87.14.94.152';
// $data = file_get_contents($url);
$xml = simplexml_load_file($url) or die("feed not loading");
// $Country = $xml->gml['featureMember']->Hostip->countryName;
// echo $Country;
echo 'BREAK HTML';
echo "-----";
echo "// "; var_dump($xml); echo " //<br/>";
?><br/><?php
var_dump($xml->gml);
?><br/><?php
print_r($xml);
?><br/><?php
var_dump((string) $xml->gml->featureMember->Hostip->countryName);
?><br/><?php
echo $xml->gml['featureMember']->Hostip->countryName;
$Country = (string) $xml->children('gml', true)
                            ->featureMember->children('', true)
                                ->Hostip->countryName; // => ITALY
echo $Country;
$fm=$xml->xpath('//gml:featureMember');
print_r($fm[0]->Hostip->countryName);

and here are the results output:

BREAK HTML-----// object(SimpleXMLElement)#1 (1) { ["@attributes"]=> array(1) { ["version"]=> string(5) "1.0.1" } } //

object(SimpleXMLElement)#2 (0) { } SimpleXMLElement Object ( [@attributes] => Array ( [version] => 1.0.1 ) ) string(0) "" ITALYSimpleXMLElement Object ( [0] => ITALY )

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.