Vous êtes sur la page 1sur 4

Creating

H
F-XC A N G
a dynamic Google sitemap using the PHP5 SimpleXML extension Page F1-XCof
HAN 4
G
PD E PD E

!
W

W
O

O
N

N
y

y
bu

bu
to

to
k

k
lic

lic
C

C
w

w
m

m
w w
w

w
o

o
.d o .c .d o .c
c u-tr a c k c u-tr a c k

Free XMLA Browser FileMaker XML Want Xml jobs?


OLAP thin-client client for Microsoft Analysis FileMaker XML/XSLT Experts Products, We've 300,000+ jobs - search for Xml job
Services Consultation, Training sheffield.
www.reportportal.com www.chapsoft.com www.WorkCircle.com/Sheffield

Home | Links Directory | Discussion Forums | Contribute

Topics Article

CSS Creating a dynamic Google sitemap using the PHP5 SimpleXML extension
JavaScript
Linux Author: Gregory Boshoff

MySQL Topic: PHP


PHP
SQL Posted on: 20th May 2006
Most Recent Audience level: Intermediate
Bookmark
This tutorial demonstates the PHP5 SimpleXML extension by using it to update a
Sponsored Links Google sitemap dynamically. It assumes that you have a basic understanding of PHP
and are familiar with XML.

PHP5's SimpleXML extension

Previous versions of PHP lacked easy to use native XML parsing functions. The
Search
SimpleXML extension which is enabled by default as of PHP5 goes some way toward
addressing this short coming.
Keyword(s):
search term
SimpleXML is designed to easy the use of working with simple XML structures.
Options: SimpleXML represents a XML file as a native PHP object and this makes it a good deal
Directory easier to work with than either DOM or SAX. SimpleXML works by parsing XML into a
Search
tree like structure in which each XML tag is an object property whose contents are
accessible. Using SimpleXML you can read, write or iterate over a XML file and access
Login it's elements and attributes.

Username: However, with the PHP SimpleXML extension you can do more than carry out basic
manipulations of a XML files elements or attributes. PHP5 also has a mechanism to
Enter username convert XML nodes between SimpleXML and the more powerful PHP5 DOM extension
Password: via the simplexml_import_dom function, which extracts a SimpleXMLElement object
from a node of a DOM document.

Login Google sitemaps

Register | Forgot Whilst there are free online sitemap generator tools available on the Internet that allow
you to create an XML sitemap which can be submitted to Google sitemaps. The majority
of these tools work by attempting to spider a entire website and this tends not to be very
accurate.

http://www.phpfive.net/article34.htm 5/13/2007
Creating
H
F-XC A N G
a dynamic Google sitemap using the PHP5 SimpleXML extension Page F2-XCof
HAN 4
G
PD E PD E

!
W

W
O

O
N

N
y

y
bu

bu
to

to
k

k
lic

lic
C

C
w

w
m

m
w w
w

w
o

o
.d o .c .d o .c
c u-tr a c k c u-tr a c k
In order to better demonstrate the SimpleXML extension to you, we will do something
useful and create a PHP function that will parse and prepend new entries dynamicly to a
web sites Google sitemap.

Site maps as I'm sure the majority of you will already know help to avoid search engine
indexing problems by letting search engine spiders know what URLs are available on
your site and how often they change.

A Google XML site map is made up of URL elements which acts as a container for the
following four child elements.

1. [loc] - The site map entries URL location.


2. [priority] - Priority - The web pages priority in a scale of 0.0 to 1.0 the highest value
being 1.0
3. [lastmod] - The date the web page was last modified in ISO 8601 format.
4. [changefreq] - The frequence that the web page is updated (always, daily, weekly,
monthly, yearly, never).

Hence a single sitemap entry for this web sites home page would look something like
this.

<?xml version="1.0" encoding="UTF-8"?>


<urlset xmlns="http://www.google.com/schemas/sitemap/0.84"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84
http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">
<url>
<loc>http://www.phpfive.net/</loc>
<priority>1.0</priority>
<lastmod>2006-05-14</lastmod>
<changefreq>daily</changefreq>
</url>
</urlset>

Each time you wish to add a new web page for indexing with Google this file must first
be amended. Naturally on a large dynamic web site this is not a task that one would
wish to undertake manually. The function below automates this task and should be
called when ever you insert a item that you wish to be indexed into your web sites
database.

<?php

/**
* Add elements to Google sitemaps.
*
* SimpleXMLElement addChild method requires PHP > 5.1.3
* If you are using a version of PHP earlier than this use you will need to the DOM extention instead.
* File permissions: Your sitemaps xml file must be writable.
*/

function prepend_sitemap ($url ='' , $sitemap_file ='sitemap.xml', $priority ='0.5' , $changefreq ='weekly')
{

http://www.phpfive.net/article34.htm 5/13/2007
Creating
H
F-XC A N G
a dynamic Google sitemap using the PHP5 SimpleXML extension Page F3-XCof
HAN 4
G
PD E PD E

!
W

W
O

O
N

N
y

y
bu

bu
to

to
k

k
lic

lic
C

C
w

w
m

m
w w
w

w
o

o
.d o .c .d o .c
c u-tr a c k c u-tr a c k

// Loads an XML file into an object


$go = simplexml_load_file ($sitemap_file );

// We are now able to manipulate the object ($go) and add a new url child element to it.
$sitemap = $go ->addChild( 'url');

// Now we create are four child elements.


$sitemap ->addChild ('loc', $url);
$sitemap ->addChild ('priority', $priority);

$sitemap ->addChild ('lastmod', date ("Y-m-d"));


$sitemap ->addChild ('changefreq', $changefreq );

/*
You may also add attributes to XML files.

For example:
$sitemap->addAttribute('total', '1.40');

Would add a total attribute with a value of 1.40 to the url element.
<url total="1.40">
*/

// Return a well-formed XML string


$xml = $go->asXML ();

// Write our string variable $xml back to the sitemap.xml file.


file_put_contents($sitemap_file , $xml);

// Destroy variable $xml


unset($xml);

return true;

} // end fun

// Example function call


prepend_sitemap ('http://www.phpfive.com/article_5.php' , 'sitemap.xml', '0.5' , 'monthly');

?>

If we where to run the script above the resulting modified sitemap.xml file would look
something like this:

<?xml version="1.0" encoding="UTF-8"?>


<urlset xmlns="http://www.google.com/schemas/sitemap/0.84"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84
http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">
<url>
<loc>http://www.phpfive.com/article_5.php</loc>
<changefreq>daily</changefreq>

http://www.phpfive.net/article34.htm 5/13/2007
Creating
H
F-XC A N G
a dynamic Google sitemap using the PHP5 SimpleXML extension Page F4-XCof
HAN 4
G
PD E PD E

!
W

W
O

O
N

N
y

y
bu

bu
to

to
k

k
lic

lic
C

C
w

w
m

m
w w
w

w
o

o
.d o .c .d o .c
c u-tr a c k
<priority>0.5</priority> c u-tr a c k

<lastmod>2006-05-14</lastmod>
<changefreq>monthly</changefreq>
</url>
</urlset>

PHP topic page

Add link to: del.icio.us | reddit | digg | technorati

OSS Fast Infoset Tools NetBeans XML Plugin Mango Solutions


Fast, compact XML without a schema Ideal XML Editor/Designer for NetBeans Eclipse- Data Analysis Solutions that deliver : S-PLUS,
for embedded devices. Evals. based SNAPs inside IDE SAS, R, XML
www.oss.com www.myeclipseide.com www.Mango-Solutions.com

Top of the page | Contact us | Accessibility | Linking to us | Legal notice

Copyright © 2006 Koodi Ltd, all rights reserved.

http://www.phpfive.net/article34.htm 5/13/2007

Vous aimerez peut-être aussi