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

熱點內容
java有哪些數組 瀏覽:984
程序員戴手錶影響工作嗎 瀏覽:235
游戲皇後解壓視頻 瀏覽:367
c語言怎麼打開文件編譯 瀏覽:436
手機上什麼app可以設計logo 瀏覽:800
pid演算法單片機 瀏覽:375
python數據精度 瀏覽:632
管什麼小女孩App 瀏覽:192
phppdf轉換成圖片 瀏覽:468
十八講pdf 瀏覽:619
mysql導入壓縮文件 瀏覽:21
usb控制單片機 瀏覽:906
你為什麼喜歡安卓手機 瀏覽:863
阿里雲伺服器購買和使用 瀏覽:389
單片機sp1 瀏覽:282
大學生玩貓咪解壓 瀏覽:794
php附近的演算法 瀏覽:346
黑馬程序員培訓機構怎麼樣 瀏覽:36
怎麼查找伺服器ip地址 瀏覽:371
華為手機為什麼開不了定位伺服器地址 瀏覽:970