Overview
|
PHP Class
|
Example
|
Award
|
Download
|
Contact
|
About
Google Site Map Example
(All in one PHPClass + Example)
A simple script that uses Google Site Map PHP Class Features
"php-tips.devquickref.com/php-tips.html", "changefreq" => "weekly", "priority" => 0.8, ); $cats[] = array( "loc" => "perl-tips.devquickref.com/perl-tips.html", "changefreq" => "weekly", "priority" => 0.8, ); $site_map_container =& new google_sitemap(); for ( $i=0; $i < count( $cats ); $i++ ) { $value = $cats[ $i ]; $site_map_item =& new google_sitemap_item( $value[ 'loc' ] , "" , $value[ 'changefreq' ] , $value[ 'priority' ] ); $site_map_container->add_item( $site_map_item ); } header( "Content-type: application/xml; charset=\"".$site_map_container->charset . "\"", true ); header( 'Pragma: no-cache' ); print $site_map_container->build(); /* Output ( console mode ): -------------------- X-Powered-By: PHP/4.3.10 Content-type: application/xml; charset="UTF-8" Pragma: no-cache
php-tips.devquickref.com/php-tips.html
weekly
0.8
perl-tips.devquickref.com/perl-tips.html
weekly
0.8
*/ /* google_sitemap.class.php This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ------------------------------------------------------------------------------- */ /** A class for generating simple google sitemaps *@author Svetoslav Marinov
*@copyright 2005 *@version 0.1 *@access public *@package google_sitemap *@link http://devquickref.com */ class google_sitemap { var $header = "<\x3Fxml version=\"1.0\" encoding=\"UTF-8\"\x3F>\n\t
"; var $charset = "UTF-8"; var $footer = "\t
\n"; var $items = array(); /** Adds a new item to the channel contents. *@param google_sitemap item $new_item *@access public */ function add_item($new_item){ //Make sure $new_item is an 'google_sitemap item' object if(!is_a($new_item, "google_sitemap_item")){ //Stop execution with an error message trigger_error("Can't add a non-google_sitemap_item object to the sitemap items array"); } $this->items[] = $new_item; } /** Generates the sitemap XML data based on object properties. *@param string $file_name ( optional ) if file name is supplied the XML data is saved in it otherwise returned as a string. *@access public *@return [void|string] */ function build( $file_name = null ) { $map = $this->header . "\n"; foreach($this->items as $item) { $item->loc = htmlentities($item->loc, ENT_QUOTES); $map .= "\t\t
\n\t\t\t
$item->loc
\n"; // lastmod if ( !empty( $item->lastmod ) ) $map .= "\t\t\t
$item->lastmod
\n"; // changefreq if ( !empty( $item->changefreq ) ) $map .= "\t\t\t
$item->changefreq
\n"; // priority if ( !empty( $item->priority ) ) $map .= "\t\t\t
$item->priority
\n"; $map .= "\t\t
\n\n"; } $map .= $this->footer . "\n"; if(!is_null($file_name)){ $fh = fopen($file_name, 'w'); fwrite($fh, $map); fclose($fh); }else{ return $map; } } } /** A class for storing google_sitemap items and will be added to google_sitemap objects. *@author Svetoslav Marinov
*@copyright 2005 *@access public *@package google_sitemap_item *@link http://devquickref.com *@version 0.1 */ class google_sitemap_item { /** Assigns constructor parameters to their corresponding object properties. *@access public *@param string $loc location *@param string $lastmod date (optional) format in YYYY-MM-DD or in "ISO 8601" format *@param string $changefreq (optional)( always,hourly,daily,weekly,monthly,yearly,never ) *@param string $priority (optional) current link's priority ( 0.0-1.0 ) */ function google_sitemap_item( $loc, $lastmod = '', $changefreq = '', $priority = '' ) { $this->loc = $loc; $this->lastmod = $lastmod; $this->changefreq = $changefreq; $this->priority = $priority; } } ?>
Copyright © 2005-2007