2011-02-12 29 views
0

我打算建立一個腳本,爲我的網站創建一個sitemap.xml,比如每天(cron將執行腳本)。我應該只是建立XML字符串並將其保存爲文件?或者使用PHP的一個類/函數/等會有一些好處。對於XML?PHP和sitemap.xml

如果我應該使用某種PHP類/函數/等,它應該是什麼?

+1

保存自己很多頭痛,只輸出一個字符串。 – 2011-02-12 23:26:11

回答

0

對於簡單的XML,輸出字符串通常比較容易。但是,文檔越複雜,使用XML庫(無論是PHP還是第三方腳本)所帶來的好處越多,因爲它將幫助您輸出正確的XML。

對於一個網站地圖,你可能是最好的只是寫字符串。

0

它是簡單的格式。幾乎沒有結構。只需輸出它作爲字符串。

0

除非您需要閱讀/使用您自己的XML站點地圖文件,只需輸出爲像其他人所說的字符串。 XML站點地圖格式非常簡單。如果你打算支持這些子類型,那麼......那麼我仍然會這樣做。

0

我建議你做cron把所有的url放在一個數組中,並將其作爲緩存存儲。然後,您可以使用此Kohana module即時生成sitemap.xml。

// this is assume you have already install the module. 
    $sitemap = new Sitemap; 

    //this is assume you put an array of all url in a cache named 'sitemap' 
    foreach($cache->get('sitemap') as $loc) 
    { 
     // New basic sitemap. 
     $url = new Sitemap_URL; 

     // Set arguments. 
     $url->set_loc($loc) 
      ->set_last_mod(1276800492) 
      ->set_change_frequency('daily') 
      ->set_priority(1); 

     // Add it to sitemap. 
     $sitemap->add($url); 
    } 

    // Render the output. 
    $response = $sitemap->render(); 

    // Cache the output for 24 hours. 
    $cache->set('sitemap', $response, 86400); 

    // Output the sitemap. 
    echo $response;