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軟件工程師的筆試題
導語:作為計算機專業畢業生的你,知道多少PHP軟件工程師的筆試題。下面的是本站小編為大家蒐集的PHP軟件工程師的筆試題,供大家參考。1.基本知識點HTTP協議中幾個狀態碼的含義:503500401200301302。。。Includerequireinclude_oncerequire_once的區別.PHP/Mysql中...
-
PHP中超全局變量$GLOBALS和global的區別
人之所以能,是相信能。努力總是會有收穫的。下面是小編整理的PHP中超全局變量$GLOBALS和global的區別,希望對大家有用,更多消息請關注應屆畢業生網。一、超全局變量$GLOBALSPHP超全局變量有很多,如下的都屬於超全局變量(Superglobal):$GLOBALS,$_SERVER,$_GET,$_POST,$_...
-
tp搜索時首頁分頁和搜索頁保持條件分頁的方法
在做搜索查詢時突然發現在首頁用的`分頁代碼在搜索頁使用時出現錯誤,首頁分頁代碼(代碼中標註start與end部分為分頁代碼),下面是由本站小編為大家整理的Thinkphp搜索時首頁分頁和搜索頁保持條件分頁的方法,喜歡的可以收藏一下!瞭解更多詳情資訊,請關注應屆畢業生...
-
2017年PHP錯誤總結範文
導語:如果你還是PHP新手的話,不妨就找些PHP錯誤點來看看。下面的是本站小編為大家蒐集的2017年PHP錯誤總結,供大家參考。錯誤總結PHP-不斷更新(新手必看)做開發請開啟全部錯誤提示:error_reporting=E_ALL|E_STRICT屏蔽錯誤提示等於掩耳盜鈴。代碼寫規範,錯誤少一半...