亚洲精品中文字幕无乱码_久久亚洲精品无码AV大片_最新国产免费Av网址_国产精品3级片

php語言

php自動生成sitemap地圖代碼

時間:2024-10-21 13:49:02 php語言 我要投稿
  • 相關(guān)推薦

php自動生成sitemap地圖代碼

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

  本節(jié)內(nèi)容:

  php自動生成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,具體實現(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ù)庫,生成URL并通過條用$s->addItem()加入到sitemap中。

  // output sitemap

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

  {

  // generate Google sitemap

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

  {

  echo $s->getGoogle();

  }

  }

  ?>

  說明:

  .htaccess文件,重定向sitemap.xml文件到sitemap.php。

  RewriteEngine on

  RewriteRule ^sitemap.xml$ sitemap.php?target=google [L]

  ping_google()函數(shù),在網(wǎng)站內(nèi)容更新的地方調(diào)用此函數(shù),用于自動通知Google網(wǎng)站地圖更新。

  代碼:

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

  <?php

  function ping_google(){

  $sitemapUrl = ‘http://www.jbxue.com/sitemap.xml’;

  $pingUrl = “http://www.google.com/webmasters/sitemaps/ping?sitemap=”.urlencode($sitemapUrl);

  $ch = curl_init();

  curl_setopt($ch, CURLOPT_URL, $pingUrl);

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  $result = curl_exec($ch) or die (curl_error()); //執(zhí)行curl請求

  //echo $result;

  curl_close($ch);

  }

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

php動態(tài)生成JavaScript代碼10-03

php生成縮略圖的兩種方法(代碼)08-08

php語言字典代碼06-08

php生成動態(tài)圖片驗證碼的一段代碼04-30

php下載代碼怎么寫07-13

PHP源代碼方式詳解08-08

20條PHP代碼優(yōu)化技巧05-06

php摘要生成函數(shù)詳解09-02

PHP生成Word文檔的方法06-28

php連接mysql數(shù)據(jù)庫代碼08-01