Reading an XML Sitemap using PHP

In this short tutorial I will cover reading a Google/XML Sitemap and grabbing hold of each URL to use as you wish. This is amazingly easy to do:

Firstly we need to pass in the Sitemap URL. In the example we’re going to use GET since this is the most useful in my opinion.

<?php

$sitemap = $_GET['sitemap'];

// Some simple validation
if (!$myxml=simplexml_load_file($sitemap)){
    echo ‘Unable to Access the Sitemap’;
}
// Loop through XML
foreach($myxml as $url)
{
       $webpage_url = $url->loc;
       // Do your thing here, store it in a database, run it against a function etc.
}
?>

This is ideal for reading a sitemap or RSS feed. Give it a try :)

Published in PHP

Leave a Comment

  • Abubaker's Comment Abubaker Posted On: Jan 9th, 2010 at 8:50 am

    Hello Anthony,
    you wrote:

    $sitemap = $_GET['sitemap'];

    the passed ['sitemap'], could it be a link to sitemap.xml located on website server?

    Regards,
    Abubaker.

  • Abubaker's Comment Abubaker Posted On: Jan 9th, 2010 at 8:57 am

    Anthony,
    one more thing..

    You wrote:

    foreach($myxml as $url)
    {
    $webpage_url = $url->loc;
    // Do your thing here, store it in a database, run it against a function etc.
    }

    how can I get the text that pointing to a link (url)?

    Regards,
    Abubaker.

Favourite Articles

"Not everything that can be counted counts, and not everything that counts can be counted."