【问题标题】:how do i get a value from simplexml_load_file?如何从 simplexml_load_file 中获取值?
【发布时间】:2026-01-01 06:10:01
【问题描述】:

您好,我需要从 Google Geocode 打印经纬度值。我已经使用以下方法检索了 xml:

$url ="http://maps.googleapis.com/maps/api/geocode/xml?address=se18lu&sensor=false";
$result = simplexml_load_file($url);

它返回以下内容:

SimpleXMLElement Object
(
    [status] => OK
    [result] => SimpleXMLElement Object
        (
            [type] => postal_code
            [formatted_address] => Bankside, London Borough of Lambeth, London SE1 8LU, UK
            [address_component] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [long_name] => SE1 8LU
                            [short_name] => SE1 8LU
                            [type] => postal_code
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [long_name] => Bankside
                            [short_name] => Bankside
                            [type] => Array
                                (
                                    [0] => sublocality
                                    [1] => political
                                )

                        )

                    [2] => SimpleXMLElement Object
                        (
                            [long_name] => London
                            [short_name] => London
                            [type] => Array
                                (
                                    [0] => locality
                                    [1] => political
                                )

                        )

                    [3] => SimpleXMLElement Object
                        (
                            [long_name] => London Borough of Lambeth
                            [short_name] => London Borough of Lambeth
                            [type] => Array
                                (
                                    [0] => administrative_area_level_3
                                    [1] => political
                                )

                        )

                    [4] => SimpleXMLElement Object
                        (
                            [long_name] => Greater London
                            [short_name] => Gt Lon
                            [type] => Array
                                (
                                    [0] => administrative_area_level_2
                                    [1] => political
                                )

                        )

                    [5] => SimpleXMLElement Object
                        (
                            [long_name] => United Kingdom
                            [short_name] => GB
                            [type] => Array
                                (
                                    [0] => country
                                    [1] => political
                                )

                        )

                )

            [geometry] => SimpleXMLElement Object
                (
                    [location] => SimpleXMLElement Object
                        (
                            [lat] => 51.5034227
                            [lng] => -0.1080750
                        )

                    [location_type] => APPROXIMATE
                    [viewport] => SimpleXMLElement Object
                        (
                            [southwest] => SimpleXMLElement Object
                                (
                                    [lat] => 51.5020958
                                    [lng] => -0.1094971
                                )

                            [northeast] => SimpleXMLElement Object
                                (
                                    [lat] => 51.5047938
                                    [lng] => -0.1067991
                                )

                        )

                    [bounds] => SimpleXMLElement Object
                        (
                            [southwest] => SimpleXMLElement Object
                                (
                                    [lat] => 51.5032242
                                    [lng] => -0.1087301
                                )

                            [northeast] => SimpleXMLElement Object
                                (
                                    [lat] => 51.5036654
                                    [lng] => -0.1075661
                                )

                        )

                )

        )

)

一切都很好。我现在需要从 xml 中检索 long 和 tat 值吗? 我如何打印/显示这些值“lat”???:例如

结果->几何->位置->纬度

【问题讨论】:

  • 和你写的差不多:$result->result->geometry->location->lat 将返回纬度。您可以通过这种方式从对象返回任何值。
  • @james_tookey 是对的。这也可以帮助php.net/manual/en/simplexml.examples-basic.php

标签: php xml api


【解决方案1】:

就像你指出的那样:

$url ="http://maps.googleapis.com/maps/api/geocode/xml?address=se18lu&sensor=false";
$result = simplexml_load_file($url);
echo $result->result->geometry->location->lat;

有时您必须将 simpleXML 对象转换为字符串。然后你可以使用:

echo (string) $result->result->geometry->location->lat;

或者

echo $result->result->geometry->location->lat->__toString();

【讨论】:

    【解决方案2】:

    您需要单独访问每个项目,而不是一次性完成。但是,您需要确保将值转换为字符串、int、float 或 w/e,因为简单 xml 元素的默认设置是使每个项目成为 SimpleXmlObject。所以对于几何的位置 lat 项目,你会这样做:

    echo "Geometry Location's Lat: ".((string)$result->result->geometry->location->lat);
    

    其余的纬度也是如此

    【讨论】:

      【解决方案3】:

      这是我使用的,部分基于this answer

      function get_geocode_arr($address)
      {
      
          $arr_location = array();
          $google_api_request_key = 'YOUR_KEY_HERE';
      
          $address    = str_replace (" ", "+", urlencode($address));
      
          $fullurl    = "https://maps.googleapis.com/maps/api/geocode/json?address=$address&sensor=true&key=$google_api_request_key";
          $string     = file_get_contents($fullurl); // get json content
          $json_res   = json_decode($string, true); //json decoder
      
          foreach ($json_res['results'][0]['address_components'] as $component)
          {
      
              switch ($component['types']) {
                  case in_array('street_number', $component['types']):
                      $arr_location['street_number'] = $component['long_name'];
                      break;
                  case in_array('route', $component['types']):
                      $arr_location['street'] = $component['long_name'];
                      break;
                  case in_array('sublocality', $component['types']):
                      $arr_location['sublocality'] = $component['long_name'];
                      break;
                  case in_array('locality', $component['types']):
                      $arr_location['locality'] = $component['long_name'];
                      break;
                  case in_array('administrative_area_level_2', $component['types']):
                      $arr_location['admin_2'] = $component['long_name'];
                      break;
                  case in_array('administrative_area_level_1', $component['types']):
                      $arr_location['admin_1'] = $component['long_name'];
                      break;
                  case in_array('postal_code', $component['types']):
                      $arr_location['postal_code'] = $component['long_name'];
                      break;
                  case in_array('country', $component['types']):
                      $arr_location['country'] = $component['long_name'];
                      break;
              }
      
          }
      
          $arr_location['location_latitude'] = $json_res['results'][0]['geometry']['location']['lat'];
          $arr_location['location_longitude'] = $json_res['results'][0]['geometry']['location']['lng'];
          $arr_location['formatted_address'] = $json_res['results'][0]['formatted_address'];
      
      
          return $arr_location;
      
      }   
      

      【讨论】: