free hit counter

Create xml file in php dynamically

xml

What are Sitemaps?

sitemaps.org :

Sitemaps are an easy way for webmasters to inform search engines about pages on their sites that are available for crawling. In its simplest form, a Sitemap is an XML file that lists URLs for a site along with additional metadata about each URL (when it was last updated, how often it usually changes, and how important it is, relative to other URLs in the site) so that search engines can more intelligently crawl the site.

Web crawlers usually discover pages from links within the site and from other sites. Sitemaps supplement this data to allow crawlers that support Sitemaps to pick up all URLs in the Sitemap and learn about those URLs using the associated metadata. Using the Sitemap protocol does not guarantee that web pages are included in search engines, but provides hints for web crawlers to do a better job of crawling your site.

Sitemap 0.90 is offered under the terms of the Attribution-Share Alike Creative Commons License and has wide adoption, including support from Google, Yahoo!, and Microsoft.

 

This example is a static XML file. A dynamic site (based on a database) has to create a dynamic Sitemap(generated and updated by a script, not manually).

 

 

  1. <?php   
  2.             $writer = new XMLWriter();  
  3.             $writer->openURI(‘sitemap.xml’); // save xml file in root dir  
  4.             $writer->startDocument(‘1.0’‘UTF-8’);  
  5.             $writer->setIndent(4);  
  6.             $writer->startElement(‘urlset’);  
  7.             $writer->writeAttribute(‘xmlns:xsi’‘http://www.w3.org/2001/XMLSchema-instance’);  
  8.             $writer->writeAttribute(‘xmlns’‘http://www.sitemaps.org/schemas/sitemap/0.9’);  
  9.             $writer->writeAttribute(‘xsi:schemaLocation’‘http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd’);  
  10.           
  11.             //$mains , get your all post from database  
  12.             $data=array(‘url’=>“full url”,‘date’=>‘post date’);  
  13.             foreach ($data as $list)  
  14.             {  
  15.                 $lin=utf8_encode($list[‘url’]);  
  16.                 $url_date = date(DATE_W3C, strtotime($list[‘date’]));  
  17.                 $writer->startElement(‘url’);  
  18.                 $writer->writeElement(‘loc’$lin);  
  19.                 $writer->writeElement(‘lastmod’,trim($url_date));  
  20.                 $writer->writeElement(‘changefreq’‘always’);  
  21.                 $writer->writeElement(‘priority’‘0.8’);  
  22.                 $writer->endElement();  
  23.             }  
  24.         $writer->endElement();  
  25.         $writer->endDocument();  
  26.     ?>  

Leave a Reply