用于Xml解析的带curl的PHP

用于Xml解析的带curl的PHP,php,xml,google-maps,curl,Php,Xml,Google Maps,Curl,我正在解析一个GoogleMapAPI来获取latlong表单地址。我成功地获取了xml。如何从xml响应中获取latlong 我的回答是 SimpleXMLElement对象([status]=>OK[result]=>SimpleXMLElement对象([type]=>Array([0]=>Location[1]=>political)[格式化地址]=>New Delhi,Delhi,India[地址分量]=>Array([0]=>SimpleXMLElement对象([long_name

我正在解析一个GoogleMapAPI来获取latlong表单地址。我成功地获取了xml。如何从xml响应中获取latlong

我的回答是

SimpleXMLElement对象([status]=>OK[result]=>SimpleXMLElement对象([type]=>Array([0]=>Location[1]=>political)[格式化地址]=>New Delhi,Delhi,India[地址分量]=>Array([0]=>SimpleXMLElement对象([long_name]=>New Delhi[type]=>Array([0]=>Location[1]=>political]))[1] =>SimpleXMLElement对象([long_name]=>West Delhi[short_name]=>West Delhi[type]=>Array([0]=>administrative_area\u level_2[1]=>political))[2]=>SimpleXMLElement对象([long_name]=>Delhi[short_name]=>DL[type]=>Array([0]=>administrative_area\u level_1[1]=>political])[3]=>SimpleXMLElement对象([long_name]=>印度[short_name]=>在[type]=>Array([0]=>country[1]=>political))[geometry]=>SimpleXMLElement对象([location]=>SimpleXMLElement对象([lat]=>28.6353080[lng]=>77.2249600)[location_type]=>近似[viewport]=>SimpleXMLElement对象([southwest]=>SimpleXMLElement对象([lat]=>28.4010669[lng]=>76.8396999[东北]=>SimpleXMLElement对象([lat]=>28.8898159[lng]=>77.3418146))[bounds]=>SimpleXMLElement对象([西南]=>SimpleXMLElement对象([lat]=>28.4010669[lng]=>76.8396999[东北]=>SimpleXMLElement对象([lat]=>28.8898159[lng]=>77.3418146)))

我想得到几何->位置->纬度值

帮我解决这个问题


提前感谢

使用DOM+XPath更容易,DOMXpath::evaluate()方法可以从xml中提取标量值:

$xml = <<<'XML'
<GeocodeResponse>
 <status>OK</status>
 <result>
  <geometry>
   <location>
    <lat>37.4217550</lat>
    <lng>-122.0846330</lng>
   </location>
   <location_type>ROOFTOP</location_type>
   <viewport>
    <southwest>
     <lat>37.4188514</lat>
     <lng>-122.0874526</lng>
    </southwest>
    <northeast>
     <lat>37.4251466</lat>
     <lng>-122.0811574</lng>
    </northeast>
   </viewport>
  </geometry>
 </result>
</GeocodeResponse>
XML;

$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXpath($dom);

$lat = $xpath->evaluate('number(/GeocodeResponse/result/geometry/location/lat)');
$lng = $xpath->evaluate('number(/GeocodeResponse/result/geometry/location/lng)');

var_dump($lat, $lng);
float(37.421755)
float(-122.084633)