av手机免费在线观看,国产女人在线视频,国产xxxx免费,捆绑调教一二三区,97影院最新理论片,色之久久综合,国产精品日韩欧美一区二区三区

php語(yǔ)言

php自動(dòng)生成sitemap地圖的代碼

時(shí)間:2025-05-23 13:43:16 php語(yǔ)言 我要投稿

php自動(dòng)生成sitemap地圖的代碼

  如何生成sitemap地圖呢?本文分享一例php代碼,用于自動(dòng)動(dòng)態(tài)生成最新的sitemap地圖文件,并通知google網(wǎng)站地圖的更新,感興趣的朋友參考下吧。

  內(nèi)容:

  php自動(dòng)生成sitemap地圖

  例子,sitemap.inc.php:主要生成sitemap的類。

  代碼:

  復(fù)制代碼 代碼示例:

  <?php

  // sitemap generator class

  class Sitemap

  {

  // constructor receives the list of URLs to include in the sitemap

  function Sitemap($items = array())

  {

  $this->_items = $items;

  }

  // add a new sitemap item

  function addItem($url,

  $lastmod = ”,

  $changefreq = ”,

  $priority = ”,

  $additional_fields = array())

  {

  $this->_items[] = array_merge(array(‘loc’ => $url,

  ‘lastmod’ => $lastmod,

  ‘changefreq’ => $changefreq,

  ‘priority’ => $priority),

  $additional_fields);

  }

  // get Google sitemap

  function getGoogle()

  {

  ob_start();

  header(‘Content-type: text/xml’);

  echo ‘<?xml version=”1.0″ encoding=”UTF-8″?>’;

  echo ‘<urlset xmlns=”http://www.sitemaps.org/schemas/sitemap/0.9″

  xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

  xsi:schemaLocation=”http://www.sitemaps.org/schemas/sitemap/0.9

  http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd”>’;

  foreach ($this->_items as $i)

  {

  echo ‘<url>’;

  foreach ($i as $index => $_i)

  {

  if (!$_i) continue;

  echo “<$index>” . $this->_escapeXML($_i) . “</$index>”;

  }

  echo ‘</url>’;

  }

  echo ‘</urlset>’;

  return ob_get_clean();

  }

  // escape string characters for inclusion in XML structure

  function _escapeXML($str)

  {

  $translation = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);

  foreach ($translation as $key => $value)

  {

  $translation[$key] = ‘&#’ . ord($key) . ‘;’;

  }

  $translation[chr(38)] = ‘&’;

  return preg_replace(“/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/”,”&#38;” ,

  strtr($str, $translation));

  }

  }

  ?>

  sitemap.php:調(diào)用sitemap.inc.php,具體實(shí)現(xiàn)sitemap。

  復(fù)制代碼 代碼示例:

  <?php

  // redirect requests to dynamic to their keyword rich versions

  require_once ‘/sitemap.inc.php’;

  define(‘SITE_DOMAIN’, ‘http://www.jbxue.com’);

  // create the Sitemap object

  $s = new Sitemap();

  // add sitemap items

  $s->addItem(SITE_DOMAIN);

  $s->addItem(SITE_DOMAIN.”/aboutus.html”);

  $s->addItem(SITE_DOMAIN.”/whatnew.php”);

  …

  //連接數(shù)據(jù)庫(kù),生成URL并通過(guò)條用$s->addItem()加入到sitemap中。

  // output sitemap

  if (isset($_GET['target']))

  {

  // generate Google sitemap

  if (($target = $_GET['target']) == ‘google’)

  {

  echo $s->getGoogle();

  }

  }

  ?>

【php自動(dòng)生成sitemap地圖的代碼】相關(guān)文章:

php自動(dòng)生成sitemap地圖代碼10-21

php自動(dòng)生成sitemap地圖的代碼07-31

PHP生成SiteMap文件的代碼09-27

PHP生成SiteMap文件代碼的方法10-30

php生成sitemap.xml的實(shí)例代碼06-02

php sitemap文件生成器代碼07-11

PHP中BB生成sitemap地圖文件的方法11-06

PHPBB生成sitemap地圖文件的方法10-15

如何實(shí)現(xiàn)PHP靜態(tài)新聞列表自動(dòng)生成代碼08-19