zak zak - 1 year ago 36
PHP Question

Reading variable from googemaps XML in PHP

I am trying to read variables from a XML output in PHP, I can read one which is good but I cannot read others? I can get the $address but not the lng and lat? What am I doing wrong?

The PHP code to get the XML is :

$xml = new SimpleXMLElement(file_get_contents("https://maps.googleapis.com/maps/api/geocode/xml?&key=&address=CV59JT&language=en-EN&sensor=false"));
$address = $xml->result->formatted_address;
$lat = $xml->geometry->lat;
$lng = $xml->geometry->lng;


And the XML output is :

<?xml version="1.0" encoding="UTF-8"?>
<GeocodeResponse>
<status>OK</status>
<result>
<type>postal_code</type>
<formatted_address>The Jordans, Coventry CV5 9JT, UK</formatted_address>
<address_component>
<long_name>CV5 9JT</long_name>
<short_name>CV5 9JT</short_name>
<type>postal_code</type>
</address_component>
<address_component>
<long_name>The Jordans</long_name>
<short_name>The Jordans</short_name>
<type>route</type>
</address_component>
<address_component>
<long_name>Coventry</long_name>
<short_name>Coventry</short_name>
<type>postal_town</type>
</address_component>
<address_component>
<long_name>West Midlands</long_name>
<short_name>West Midlands</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>England</long_name>
<short_name>England</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>United Kingdom</long_name>
<short_name>GB</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>52.4141128</lat>
<lng>-1.5580964</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>52.4127592</lat>
<lng>-1.5598679</lng>
</southwest>
<northeast>
<lat>52.4154571</lat>
<lng>-1.5571700</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>52.4137181</lat>
<lng>-1.5593655</lng>
</southwest>
<northeast>
<lat>52.4144982</lat>
<lng>-1.5576724</lng>
</northeast>
</bounds>
</geometry>
<place_id>ChIJ5YQ9ljhLd0gR6sZ-dOK8tNI</place_id>
</result>
</GeocodeResponse>


I know I am not reading it in the correct way but how do I fix it?

Many thanks

Answer Source

If you format your xml properly, like this:

$s = '<?xml version="1.0" encoding="UTF-8"?>
<GeocodeResponse>
<status>OK</status>
<result>
    <type>postal_code</type>
    <formatted_address>The Jordans, Coventry CV5 9JT, UK</formatted_address>
    <address_component>
        <long_name>CV5 9JT</long_name>
        <short_name>CV5 9JT</short_name>
        <type>postal_code</type>
    </address_component>
    <address_component>
        <long_name>The Jordans</long_name>
        <short_name>The Jordans</short_name>
        <type>route</type>
    </address_component>
    <address_component>
        <long_name>Coventry</long_name>
        <short_name>Coventry</short_name>
        <type>postal_town</type>
    </address_component>
    <address_component>
        <long_name>West Midlands</long_name>
        <short_name>West Midlands</short_name>
        <type>administrative_area_level_2</type>
        <type>political</type>
    </address_component>
    <address_component>
        <long_name>England</long_name>
        <short_name>England</short_name>
        <type>administrative_area_level_1</type>
        <type>political</type>
    </address_component>
    <address_component>
        <long_name>United Kingdom</long_name>
        <short_name>GB</short_name>
        <type>country</type>
        <type>political</type>
    </address_component>
    <geometry>
        <location>
            <lat>52.4141128</lat>
            <lng>-1.5580964</lng>
        </location>
        <location_type>APPROXIMATE</location_type>
        <viewport>
            <southwest>
                <lat>52.4127592</lat>
                <lng>-1.5598679</lng>
            </southwest>
            <northeast>
                <lat>52.4154571</lat>
                <lng>-1.5571700</lng>
            </northeast>
        </viewport>
        <bounds>
            <southwest>
                <lat>52.4137181</lat>
                <lng>-1.5593655</lng>
            </southwest>
            <northeast>
                <lat>52.4144982</lat>
                <lng>-1.5576724</lng>
            </northeast>
        </bounds>
    </geometry>
    <place_id>ChIJ5YQ9ljhLd0gR6sZ-dOK8tNI</place_id>
</result>
</GeocodeResponse>';

You will see that path to lat/lng can be:

$lat = $xml->result->geometry->location->lat;    // 52.4141128
$lng = $xml->result->geometry->location->lng;    // -1.5580964

Or

$lat = $xml->result->geometry->viewport->southwest->lat;    // 52.4127592
$lng = $xml->result->geometry->viewport->southwest->lng;    // -1.5598679

Other values you can receive using the same technique.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download