SimpleXML, Namespaces and hostip.info

Hi,

I’m trying to get the <countryAbbrev> using SimpleXML from http://api.hostip.info/?ip=188.222.185.221 There are namespaces so I’m using children() but I just can’t seem to get deep enough. I know there are workarounds but I want to try to do it properly so I know where I’m going wrong.

Any ideas?

Thanks

I did this recently with yahoo weather. Try this:


$xml = new SimpleXMLElement('http://api.hostip.info/?ip=188.222.185.221',LIBXML_NOCDATA,true);
$xml->registerXPathNamespace('f', 'http://www.opengis.net/gml');
$result = $xml->xpath('//f:yourelementname');

$result is now an array of SimpleXMLElement objects that match yourelementname.

StarLion,

Thanks for your help, I’m getting closer with


$xml = new SimpleXMLElement('http://api.hostip.info/?ip=188.222.185.221',LIBXML_NOCDATA,true);
$xml->registerXPathNamespace('f', 'http://www.opengis.net/gml');
$result = $xml->xpath('//f:featureMember'); 
print_r($result);

Which gives me


Array
(
    [0] => SimpleXMLElement Object
        (
            [Hostip] => SimpleXMLElement Object
                (
                    [ip] => 188.222.185.221
                    [countryName] => UNITED KINGDOM
                    [countryAbbrev] => UK
                    [comment] => SimpleXMLElement Object
                        (
                        )
 
                    [ipLocation] => SimpleXMLElement Object
                        (
                        )
 
                )
 
        )
 
)

but I just can’t seem to get to countryAbbrev. I’ve tried


$result = $xml->xpath('//f:featureMember')->countryAbbrev; 

and it come up empty

realise my las line was wrong and i should be something like $country = $result[“countryAbbrev”] but I still cant get there.


[COLOR="Red"]<-- You are here ...[/COLOR]
Array
(
  [0] => SimpleXMLElement Object
  (
    [COLOR="Red"]<-- ... so here is $result[0] ...[/COLOR]
    [Hostip] => SimpleXMLElement Object
    (
      [COLOR="Red"]<-- ... here is $result[0]['Hostip'] ...[/COLOR]
      [ip] => 188.222.185.221
      [countryName] => UNITED KINGDOM
      [countryAbbrev] => UK  [COLOR="Red"]<-- ... and that is $result[0]['Hostip']['CountryAbbrev'][/COLOR]
      [comment] => SimpleXMLElement Object
        (
        )
      [ipLocation] => SimpleXMLElement Object
        (
        )
    )
  )
)

Does that make sense?

ScallioXTX,

Thanks, it does make sense and I tried that too.


$res = $result[0]['Hostip']['CountryAbbrev'];
echo $res;

but it’s still coming up empty

Ah, CountryAbbrev starts with a lower case “c” : countryAbbrev

My bad :slight_smile:

Sorry to have to keep coming back but it’s still empty. I copied and pasted from the print_r($result); output to avoid typos

Yikes. At index 0 there actually is a SimpleXMLElement. I missed that.
It should be:


[COLOR="Red"]<-- You are here ...[/COLOR]
Array
(
  [0] => SimpleXMLElement Object
  (
    [COLOR="Red"]<-- ... so here is $result[0] ...[/COLOR]
    [Hostip] => SimpleXMLElement Object
    (
      [COLOR="Red"]<-- ... here is $result[0]->Hostip ...[/COLOR]
      [ip] => 188.222.185.221
      [countryName] => UNITED KINGDOM
      [countryAbbrev] => UK  [COLOR="Red"]<-- ... and that is $result[0]->Hostip->countryAbbrev[/COLOR]
      [comment] => SimpleXMLElement Object
        (
        )
      [ipLocation] => SimpleXMLElement Object
        (
        )
    )
  )
)

:headbang:

hallelujah, Thanks a lot ScallioXTX, it’s been getting to me all day.

Do you only want the countryAbbrev? If so, then you could simply (pardon the pun) do:


$xml = new SimpleXMLElement('http://api.hostip.info/?ip=188.222.185.221', null, true);
$countries = $xml->xpath('//countryAbbrev');
$country = $countries[0];

Thanks Salathe