php自動生成sitemap地圖的代碼
如何生成sitemap地圖呢?本文分享一例php代碼,用於自動動態生成最新的sitemap地圖文件,並通知google網站地圖的更新,感興趣的`朋友參考下吧。
php自動生成sitemap地圖
例子,:主要生成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=”″
xmlns:xsi=””
xsi:schemaLocation=”
”>’;
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};)/”,”&” ,
strtr($str, $translation));
}
}
?>
:調用,具體實現sitemap。
複製代碼 代碼示例:
<?php
// redirect requests to dynamic to their keyword rich versions
require_once ‘/’;
define(‘SITE_DOMAIN’, ‘’);
// create the Sitemap object
$s = new Sitemap();
// add sitemap items
$s->addItem(SITE_DOMAIN);
$s->addItem(SITE_DOMAIN.”/”);
$s->addItem(SITE_DOMAIN.”/”);
…
//連接數據庫,生成URL並通過條用$s->addItem()加入到sitemap中。
// output sitemap
if (isset($_GET['target']))
{
// generate Google sitemap
if (($target = $_GET['target']) == ‘google’)
{
echo $s->getGoogle();
}
}
?>
説明:
cess文件,重定向文件到。
RewriteEngine on
RewriteRule ^$ [L]
ping_google()函數,在網站內容更新的地方調用此函數,用於自動通知Google網站地圖更新。
代碼:
複製代碼 代碼示例:
<?php
function ping_google(){
$sitemapUrl = ‘’;
$pingUrl = “($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服務器網站內容更新。
-
作為程序員必知的16個最佳PHP庫
PHP是一種功能強大的web站點腳本語言,通過PHP,web網站開發者可以更容易地創建動態的引人入勝的web頁面。開發人員可以使用PHP代碼與一些網站模板和框架來提升功能和特性。然而,編寫PHP代碼是一個繁瑣又耗時的過程。為了縮短開發時間,開發人員可以用PHP庫替代編寫代...
-
PHP讀取文件的正確方法
處理諸如PHP之類的現代編程語言的樂趣之一就是有大量的選項可用。PHP讀取文件的正確方法是怎麼樣的呢?下面是小編為大家整理的PHP讀取文件的正確方法,歡迎參考~傳統的fopen方法fopen方法可能是以前的C和C++程序員最熟悉的,因為如果您使用過這些語言,那麼它們或多或...
-
PHP框架的概念
PHP框架為各種技術等級的開發者減少了重複編碼的需求、加快了開發進程,並在創建web應用時確保正確的編碼。下面是小編分享的PHP框架的概念,歡迎大家參考!PHP成為世界上最流行的腳本語言有許多原因:靈活性,易用性等等。但通常只用PHP或者其他語言編碼就會顯得單調、...
-
php遞歸創建和刪除文件夾的代碼
php中遞歸創建和刪除文件夾的代碼,供大家學習參考。就跟隨本站小編一起去了解下吧,想了解更多相關信息請持續關注我們應屆畢業生考試網!方法一複製代碼代碼如下:<?php/***目錄生成類:UtilsMakeDir*@authoryepeng*@since2010.3.18*/classUtilsMakeDir{//基目錄建...