導航:首頁 > 編程語言 > php生成網站地圖

php生成網站地圖

發布時間:2023-11-16 14:19:25

『壹』 php怎麼生成sitemap.xml

/**
*生成站點地圖
*/
classsitemap{
private$sitemapFile=array();
private$oldxml=null;
private$newxml=null;
public$error=null;
publicfunction__construct($sitemapFile){
$this->sitemapFile=$sitemapFile;
if(is_file($this->sitemapFile)){
$data=file_get_contents($this->sitemapFile);
if($data){
$this->oldxml=newSimpleXMLElement($data);
}else{
$this->error='讀取站點地圖文件失敗';
}
}else{
$this->oldxml=$this->createEmptySitemap();
}
$this->newxml=$this->createEmptySitemap();
}
(){
$str='<?xmlversion="1.0"encoding="UTF-8"?>';
$str.='<urlsetxmlns="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.9http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"></urlset>';
returnnewSimpleXMLElement($str);
}
publicfunctionaddChilds($urlArr){
$urlArr=(array)$urlArr;
foreach($urlArras$url){
$priority=0.5;
$lastmod=date('Y-m-d');
$changefreq='weekly';
if(stripos($url,'.html')){
$priority=1;
$changefreq='monthly';
}
if($oldXmlUrl=$this->findOldXmlUrl($url)){
$priority=$oldXmlUrl->priority;
$lastmod=$oldXmlUrl->lastmod;
$changefreq=$oldXmlUrl->changefreq;
}
$rating=$this->newxml->addChild('url');
$rating->addChild('loc',$url);
$rating->addChild('priority',$priority);
$rating->addChild('lastmod',$lastmod);
$rating->addChild('changefreq',$changefreq);
}
}
publicfunctionfindOldXmlUrl($url){
$oldXmlUrl='';
foreach($this->oldxml->urlas$key=>$xmlUrl){
if($xmlUrl->loc==$url){
$oldXmlUrl=$xmlUrl;
unset($this->oldxml->url[$key]);
break;
}
}
return$oldXmlUrl;
}
publicfunctionsave(){
$data=$this->newxml->asXML();
if(file_put_contents($this->sitemapFile,$data)===false){
$this->error='寫入站點地圖數據失敗';
returnfalse;
}
returntrue;
}
}

上面這個是我個人博客生成站點地圖用的類。

客戶端調用代碼如下:

$sitemapFile='Sitemap.xml';
$sitemap=newsitemap($sitemapFile);

if($sitemap->error){
die($sitemap->error);
}

$newUrl=[
'http://www.kiscms.com/content/28.html'
];

$sitemap->addChilds();

if(!$sitemap->save()){
die($sitemap->error);
}

關鍵的問題是,你如何得到整站的url呢?

我個人博客的解決方法是寫了個蜘蛛程序爬出來的。

『貳』 php生成百度移動Sitemap協議XML格式文件,其中有兩個標簽不知怎樣加進去<mobile:mobile type="mobile"/>

打開愛站SEO工具包,找到網站地圖/sitemap,點擊進入登錄或注冊頁面,如果所示1

登錄之後,點擊添加網站,輸入網站域名www.meng10000.com,點擊保存,如圖所示2

點擊默認分類下面的網站域名,設置基本信息和抓取更新頻率後,點擊抓爬,如圖所示3和4

接下來等待抓爬完成或者自己手動點擊停止,查看,生成XML文件,如圖所示5和6

然後,打開文件目錄,找到對應sitemap.xml文件和robots.txt文件,將其全部上傳到網站伺服器根目錄下。

6
最後,打開網路站長平台,鏈接提交,提交sitemap地址,之後就等待網路蜘蛛抓取收錄你的網站吧,如圖所示。

『叄』 怎麼用php實現自動生成站點地圖的XML文件

需要做的就是新建一個地圖模板,模板-增加自定義頁面模板。

在瀏覽器裡面輸入:域名/sitemap.html,就能夠看到網站地圖了。

閱讀全文

與php生成網站地圖相關的資料

熱點內容
伺服器與ups用什麼線 瀏覽:967
unix網路命令 瀏覽:48
程序員表白代碼大全可復制 瀏覽:365
手機如何共享web伺服器 瀏覽:956
php介面有什麼用 瀏覽:382
iis如何安裝php 瀏覽:791
k5嗜血魔鍵安卓怎麼調好用 瀏覽:834
建行app中如何添加銀行卡 瀏覽:281
簡便演算法100點 瀏覽:161
如何創新我的世界伺服器 瀏覽:881
戰地怎麼看伺服器地址 瀏覽:348
vue怎麼打包放上伺服器 瀏覽:165
為什麼安卓服夏日活動沒有兔子頭 瀏覽:894
pubg為什麼顯示伺服器連接失敗 瀏覽:650
阿里雲掃碼登錄伺服器 瀏覽:971
化學基礎pdf 瀏覽:896
51單片機晶碼管 瀏覽:281
怎麼查伺服器假死原因日誌在哪看 瀏覽:277
掃描pdf文件 瀏覽:926
解壓密碼百度雲在線解壓 瀏覽:767