PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store
Related Articles

Geocoding with PHP and the Google Maps API

Accessing the Geocoder Data Using SimpleXML

Now that we know how to retrieve a geocoder response and what the returned XML actually means, we can parse the XML so we can use the data as required.

To parse the XML we are going to use SimpleXML, which as its name suggests, makes the use of XML in PHP very simple. SimpleXML has been available natively in PHP since PHP 5.

To read the values in an XML file, all we need to do is instantiate the SimpleXMLElement class, passing to its constructor the XML string we want to parse. Listing 3 shows an example of how this is achieved.

Listing 3 Creating a new instance of the SimpleXMLElement class (listing-3.php)
<?php
    $str = '<kml> <Response> .... </Response> </kml'; // sample XML file
    $xml = new SimpleXMLElement($str);
?>

Once you have the SimpleXMLElement object, you can access any element in the XML file just as you would with normal PHP object properties. Returning the geocoder response from earlier in this article, you can access the status code of the request using the following code:

Listing 4 Access the geocoder response status code (listing-4.php)
<?php
    $xml = new SimpleXMLElement($geocoderResponse);
    echo $xml->Response->Status->code;
?>

In actual fact, the type of $xml->Response->Status->code is another instance of SimpleXMLElement. Therefore, you should really be casting this value to the type required (such as a string or an integer).

Listing 5 shows an example of reading the status code and ensuring the request completed successfully (remember that a response code of 200 means the request was successful).

Listing 5 Casting the response status code to an integer and ensuring the request was successful (listing-5.php)
<?php    
    $xml = new SimpleXMLElement($geocoderResponse);
    $status = (int) $xml->Response->Status->code;
 
    if ($status == 200) {
        // geocoder request completed successfully
    }
?>

We now know how to read the values within XML tags, but some data will be held in attributes rather than as tag values. One such example is the Accuracy attribute for each Placemark element. To access an attribute when using SimpleXML you treat its parent element as an array.

Listing 6 shows how we can loop over each placemark in the response and then access its Accuracy value accordingly, as well as outputting the properly formatted address.

Listing 6 Looping over placemarks and finding the respective placemark accuracy (listing-6.php)
<?php
    $xml = new SimpleXMLElement($geocoderResponse);
 
    foreach ($xml->Response->Placemark as $placemark) {
        $accuracy = (int) $placemark->AddressDetails['Accuracy'];
 
        if ($accuracy == 8) {
            // address level accuracy
        }
 
        echo $placemark->address . "\n";
    }
?>

At this point we have covered all of the base knowledge required to query the geocoder, understand the response, and read specific values from the returned XML using SimpleXML.

We will now implement a fully functioning geocoder using PHP 5 that combines everything we have covered so far in this chapter.

In This Article


Additional Files