RSS
You are here: Home » Ant's Highlights » Reading an XML Sitemap using PHP

Reading an XML Sitemap using PHP

Sat, Aug 23, 2008

Ant's Highlights, Internet, PHP

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 :)

This post was written by:

Anthony - who has written 137 posts on Anthony Shapley.


Contact the author

Leave a Reply