php自動生成sitemap地圖代碼_第1頁
php自動生成sitemap地圖代碼_第2頁
php自動生成sitemap地圖代碼_第3頁
php自動生成sitemap地圖代碼_第4頁
php自動生成sitemap地圖代碼_第5頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

本文格式為Word版,下載可任意編輯——php自動生成sitemap地圖代碼php自動生成sitemap地圖代碼

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

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

php自動生成sitemap地圖

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

代碼:

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

?php

//sitemapgeneratorclass

classSitemap

//constructorreceivesthelistofURLstoincludeinthesitemap

functionSitemap$items=array

$this-_items=$items;

//addanewsitemapitem

functionaddItem$url,

$lastmod=',

$changefreq=',

$priority=',

$additional_fields=array

$this-_items[]=array_mergearrayloc=$url,

lastmod=$lastmod,

changefreq=$changefreq,

priority=$priority,

$additional_fields;

//getGooglesitemap

functiongetGoogle

ob_start;

headerContent-type:text/xml;

echo?xmlversion='1.0encoding='UTF-8?;

echourlsetxmlns='http://./schemas/sitemap/0.9

xmlns:xsi='http://./2022/XMLSchema-instance'

xsi:schemaLocation='http://./schemas/sitemap/0.9

http://./schemas/sitemap/0.9/sitemap.xsd';

foreach$this-_itemsas$i

echourl;

foreach$ias$index=$_i

if!$_icontinue;

echo"$index'.$this-_escapeXML$_i."/$index';

echo/url;

echo/urlset;

returnob_get_clean;

//escapestringcharactersforinclusioninXMLstructure

function_escapeXML$str

$translation=get_html_translation_tableHTML_ENTITIES,ENT_QUOTES;

foreach$translationas$key=$value

$translation[$key]=#.ord$key.;;

$translation[chr38]=;

returnpreg_replace"/?![A-Za-z]0,4\w2,3;|#[0-9]2,3;/','#38;',

strtr$str,$translation;

?

sitemap.php:調(diào)用sitemap.inc.php,概括實現(xiàn)sitemap。

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

?php

//redirectrequeststodynamictotheirkeywordrichversions

require_once/sitemap.inc.php;

defineSITE_DOMAIN,http://.;

//createtheSitemapobject

$s=newSitemap;

//addsitemapitems

$s-addItemSITE_DOMAIN;

$s-addItemSITE_DOMAIN.'/aboutus.html';

$s-addItemSITE_DOMAIN.'/whatnew.php';

//連接數(shù)據(jù)庫,生成URL并通過條用$s-addItem參與到sitemap中。

//outputsitemap

ifisset$_GET[target]

//generateGooglesitemap

if$target=$_GET[target]==google

echo$s-getGoogle;

?

說明:

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

RewriteEngineon

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

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

代碼:

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

?php

functionping_google

$sitemapUrl=http://./sitemap.xml;

$pingUrl="http://./webmasters/sitemaps/ping?sitemap='.urlencode$sitemapUrl;

$ch=curl_init;

curl_setopt$ch,CURLOPT_URL,$pingUrl;

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論