File_Sitemap
[ class tree: File_Sitemap ] [ index: File_Sitemap ] [ all elements ]

Source for file sitemap_example.php

Documentation is available at sitemap_example.php

  1. <?php
  2.  
  3. require_once "File/Sitemap.php";
  4. require_once "File/Sitemap/Index.php";
  5.  
  6. // Since dom is a protected member, we create
  7. // this little utility class to show an example
  8. // of the generated output.
  9. class Sitemap_Example extends File_Sitemap
  10. {
  11.     function output()
  12.     {
  13.         $this->dom->formatOutput = true;
  14.         $sm $this->dom->saveXML();
  15.         $sm htmlentities($sm);
  16.         echo "<pre>";
  17.         echo $sm;
  18.         echo "</pre>\n";
  19.     }
  20. }
  21.  
  22. // Create sitemap object
  23. // $sm = new File_Sitemap();
  24. $sm = new Sitemap_Example();
  25.  
  26. // Let define some urls
  27. $baseurl 'http://pear.php.net';
  28. $urls = array('/',
  29.     '/packages.php',
  30.     '/manual/',
  31.     '/manual/en/',
  32.     '/manual/en/preface.php',
  33.     '/pepr/',
  34.     '/pepr/pepr-proposal-show.php?id=555',
  35. );
  36.  
  37. // A function to generate an arbitrary priority number...
  38. function priority($url)
  39. {
  40.     $a = array();
  41.     $n = 0;
  42.     $n += preg_match_all('/\.php/'$url$a);    // url contains .php
  43.     $n += preg_match_all('/\//'$url$a);        // number of /
  44.     $n += preg_match_all('/\?/'$url$a);        // url contains ?
  45.  
  46.     $p = 1 / $n;
  47.  
  48.     return $p;
  49. }
  50.  
  51. // Add urls to our sitemap
  52. foreach ($urls as $url{
  53.     $sm->add($baseurl.$urlpriority($url));
  54. }
  55.  
  56. // Add some precisions for specific pages
  57. $sm->add($baseurl.'/'NULL'daily');
  58. $sm->add($baseurl.'/manual/'NULL'weekly');
  59.  
  60. // Validate our sitemap (not really needed is we used the API to generate it!)
  61. // $sm->validate();
  62.  
  63. // Test validity of all urls in the sitemap
  64. // This could take a very long time if sitemap is huge...
  65. // $sm->test();
  66.  
  67. // Save sitemap to compressed file
  68. // $sm->save('/path/to/web/root/sitemap1.gz');
  69.  
  70. // Notify Google about our sitemap update
  71. // $sm->notify('http://my.web.site/sitemap1.gz');
  72.  
  73. // This is our sitemap: (output is not a function of File_Sitemap class!)
  74. $sm->output();
  75.  
  76.  
  77. // Not an example with sitemap index
  78. $smi = new File_Sitemap_Index();
  79.  
  80. $sitemaps = array('http://my.web.site/sitemap1.gz',
  81.                   'http://my.web.site/sitemap2.gz',
  82.                   'http://my.web.site/sitemap3.gz',
  83. );
  84.  
  85. $smi->add($sitemaps);
  86.  
  87. // It's a good idea to ensure that all sitemaps are reacheable...
  88. // $smi->test();
  89.  
  90. // Save the sitemap index
  91. // $smi->save('/path/to/wesite/root/sitemap.gz')
  92.  
  93. ?>

Documentation generated on Mon, 11 Mar 2019 15:46:43 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.