<pre id="bbfd9"><del id="bbfd9"><dfn id="bbfd9"></dfn></del></pre>

          <ruby id="bbfd9"></ruby><p id="bbfd9"><mark id="bbfd9"></mark></p>

          <p id="bbfd9"></p>

          <p id="bbfd9"><cite id="bbfd9"></cite></p>

            <th id="bbfd9"><form id="bbfd9"><dl id="bbfd9"></dl></form></th>

            <p id="bbfd9"><cite id="bbfd9"></cite></p><p id="bbfd9"></p>
            <p id="bbfd9"><cite id="bbfd9"><progress id="bbfd9"></progress></cite></p>
            php語言

            php自動生成sitemap地圖的代碼

            時間:2025-04-18 16:15:22 php語言 我要投稿

            php自動生成sitemap地圖的代碼

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

              php自動生成sitemap地圖

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

              代碼:

              復制代碼 代碼示例:

              <?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:調用sitemap.inc.php,具體實現sitemap。

              復制代碼 代碼示例:

              <?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”);

              …

              //連接數據庫,生成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()函數,在網站內容更新的地方調用此函數,用于自動通知Google網站地圖更新。

              代碼:

              復制代碼 代碼示例:

              <?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()); //執行curl請求

              //echo $result;

              curl_close($ch);

              }

              注意:此函數需要開啟php的curl模塊。

              將以上代碼加入到網站中,即可實現動態自動生成sitemap文件了,而且能夠實時通知Google服務器網站內容更新。

            【php自動生成sitemap地圖的代碼】相關文章:

            php自動生成sitemap地圖代碼05-23

            php自動生成sitemap地圖的代碼05-23

            PHP生成SiteMap文件的代碼03-05

            PHP生成SiteMap文件代碼的方法03-01

            php生成sitemap.xml的實例代碼06-02

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

            PHP中BB生成sitemap地圖文件的方法12-14

            PHPBB生成sitemap地圖文件的方法04-08

            如何實現PHP靜態新聞列表自動生成代碼04-25

                    <pre id="bbfd9"><del id="bbfd9"><dfn id="bbfd9"></dfn></del></pre>

                    <ruby id="bbfd9"></ruby><p id="bbfd9"><mark id="bbfd9"></mark></p>

                    <p id="bbfd9"></p>

                    <p id="bbfd9"><cite id="bbfd9"></cite></p>

                      <th id="bbfd9"><form id="bbfd9"><dl id="bbfd9"></dl></form></th>

                      <p id="bbfd9"><cite id="bbfd9"></cite></p><p id="bbfd9"></p>
                      <p id="bbfd9"><cite id="bbfd9"><progress id="bbfd9"></progress></cite></p>
                      飘沙影院