導航:首頁 > 編程語言 > 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生成網站地圖相關的資料

熱點內容
微信應用鎖加密 瀏覽:451
數字英雄對程序員的影響 瀏覽:761
程序員培訓學校排名 瀏覽:586
oppo手機如何解開微信的加密 瀏覽:37
學校論文答辯源碼 瀏覽:66
rp在單片機中 瀏覽:178
linux軟體運行命令 瀏覽:354
stcid加密程序 瀏覽:139
把解壓包子放到水裡 瀏覽:286
phpmongodb連接池 瀏覽:945
日本解壓捏泡泡紙 瀏覽:121
環保參比演算法 瀏覽:413
解壓中的刪掉是什麼意思 瀏覽:766
王牌競速什麼時候能停止維修伺服器 瀏覽:488
pdf閱讀器官方 瀏覽:88
程序員那麼愛心 瀏覽:305
字元a經過md5加密 瀏覽:420
綠色的小蝴蝶是個什麼app 瀏覽:18
python編程輸入數字輸出年月日英文 瀏覽:631
程序員槍手 瀏覽:748