2011-02-12 27 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;