Tutorials - PHP Tips & Tricks - Snazzy Automatic Sitemap Generation with Images in PHP - - Refresh Creations Ltd

Online Tutorials

FREE Web Design Quote

Tell us about your project and get your web design quote here.

Quote Request Form

Contact Refresh:

158 Torquay Road
Paignton
Torbay
Devon
TQ3 2AH

Email:

hello@refreshcreations.co.uk

Phone: 01803 558 978
Fax: 0870 80 30 989
Google Maps Click Here

Featured Website Project

Client: Whispurr Luxury Lingerie

Description:
An adult themed e-commerce website selling lingerie of many varieties. Built using a heavily customised oscommerce solution to allow the site to be updated with greater ease and simplicity. This includes the ability to send out newsletter, use an automatically optimized, auto-pinging blog with wordpress like features and url renaming. User customisable product meta tags for extended SEO performance. Simple competition are available to site visitors through the interface and registered users may create wish lists which they can then publicise or simply send out to their friends....

View Website

Website Design News

More News

Snazzy Automatic Sitemap Generation with Images in PHP

Snazzy Automatic Sitemap Generation with Images in PHP

Using a few lines of code you can automate the creation of your sitemaps, with images embedded with the following code sample:

Firstly, we need to set-up our main variables, including the server name and the header area of our sitemap file.

These will be contained in $server and $header for simplicity.

$server = "http://www.yourserver.com/";
$output="";
$xoutput = "\n\n";

Next we'll need a function to get our images with details from our website.


function imagesfromtext($url){
global $server;


if ($url != ""){
$output="";
echo "<h3>".$url."</h3>";
$xmlStr = file_get_contents($url);

if ($xmlStr != NULL){
$doc=new DOMDocument();
$doc->loadHTML($xmlStr);

if ($doc != NULL){
$xml=simplexml_import_dom($doc);
$images=$xml->xpath('//img');

foreach ($images as $img) {
$output.= "<image:image>\n";
$output.= "<image:loc>".$img['src']."</image:loc>\n";
$output.= "<image:title>".$img['alt']."</image:title>\n";
$output.= "</image:image>\n";
$total++;
}


}
}
}

return $output;

}

Now, we'll place our webpages in an array, with a page title.


$url[] = "index.html";
$name[] = "Home Page";

$url[] = "contact.html";
$name[] = "Contact Page";

$url[] = "gallery.html";
$name[] = "My Gallery";

After our pages have been added, we'll now want to loop through the array, grabbing the images from it, appending data to our sitemap file for each page.


for ($i=0; $i<count($url); $i++){
$output.= $server.$url[$i]."\n";
$xoutput.= "<url><loc>".$server.$url[$i]."</loc>\n".imagesfromtext($server.$url[$i])."\n</url>\n";
}

$xoutput.="</urlset>\n";

Finally. We'll need to write all this out to a well named file on our server, in this case, lets call it sitemap.xml


$filename="sitemap.xml";


if (is_writable($filename)) {

if (!$handle = fopen($filename, 'w')) {
exit;
}

if (fwrite($handle, $xoutput) === FALSE) {
exit;
}

fclose($handle);

} else {
echo "File not created, please upload a file called ".$filename;
}

This script should help you out with using googles newer features for sitemaps although some improvements will be useful to add-on, including the following:

  • Setting up a blocklist of standard images used on every page.
  • Setting up a cron-job to run the script once every so-often would also help any sitemap related issues. As would using a form of crawler or script to automatically grab the details of each page on your server space.

Download the FULL Script here

Viewed 1284 times since 15 Apr 2010

Return to Tutorials Index

Why Refresh Creations?

All our websites we create are secure and customer editable.
From small, brochure websites to large-scale e-commerce and social-network sites.

We tailor our admin system to your website for a simple-to-use, effective form of website management.

Contact Now
ask refresh creations about website design

Webmaster Tutorials

Email Account Setup

Difficulty: Easy

View Tutorial

Recent Tweets

Contact Refresh Creations Now on 0845 09 42 987

Legal
Web Design Services
Refresh Creations Website Design
  • Follow us on Twitter
  • Follow us on Facebook
  • Follow us on Youtube
  • Find us on LinkedIn