hostip.info - 使用SimpeXML解析API响应

时间:2011-01-28 12:27:12

标签: php xml api simplexml

我正在尝试使用http://api.hostip.info/?ip=12.215.42.19解析SimpleXML的xml响应,但我似乎无法使其正常工作。

响应

<?xml version="1.0" encoding="ISO-8859-1" ?>
<HostipLookupResultSet version="1.0.1" xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.hostip.info/api/hostip-1.0.1.xsd">
 <gml:description>This is the Hostip Lookup Service</gml:description>
 <gml:name>hostip</gml:name>
 <gml:boundedBy>
  <gml:Null>inapplicable</gml:Null>
 </gml:boundedBy>
 <gml:featureMember>

  <Hostip>
   <ip>12.215.42.19</ip>
   <gml:name>Sugar Grove, IL</gml:name>
   <countryName>UNITED STATES</countryName>
   <countryAbbrev>US</countryAbbrev>
   <!-- Co-ordinates are available as lng,lat -->
   <ipLocation>

    <gml:pointProperty>
     <gml:Point srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">
      <gml:coordinates>-88.4588,41.7696</gml:coordinates>
     </gml:Point>
    </gml:pointProperty>
   </ipLocation>
  </Hostip>
 </gml:featureMember>

</HostipLookupResultSet>

有人可以帮助我访问例如Hostip&gt; ipLocation&gt; pointProperty&gt; Point&gt;坐标

提前致谢!

2 个答案:

答案 0 :(得分:2)

以下是两种方式(可通过搜索在其他地方获得!)。

XPath (非常基础的演示)

$coords = $xml->xpath('//gml:coordinates');
echo $coords[0];

<强>&#34;简单&#34; XML (不那么简单)

echo $xml
    ->children('gml', TRUE)->featureMember
    ->children('', TRUE)->Hostip->ipLocation
    ->children('gml', TRUE)->pointProperty->Point->coordinates;

答案 1 :(得分:-1)

您可以访问数组之类的属性 http://www.electrictoolbox.com/php-simplexml-element-attributes/ 喜欢(我不确定你的例子)

Hostip->ipLocation->gml['pointproperty']