PHP和sitemap.xml

时间:2011-02-12 23:23:06

标签: php xml xml-serialization

我打算构建一个脚本,为我的网站创建一个sitemap.xml,比如每天(cron将执行脚本)。我应该只构建XML字符串并将其另存为文件吗?或者使用PHP的类/函数/等会有一些好处。对于XML?

如果我应该使用某种PHP类/函数/等,应该是什么?

4 个答案:

答案 0 :(得分:0)

对于简单的XML,通常更容易输出字符串。但是,文档越复杂,使用XML库(包括PHP或第三方脚本)就会带来的好处就越多,因为它可以帮助您输出正确的XML。

对于站点地图,您可能最好只写字符串。

答案 1 :(得分:0)

这是简单的格式。几乎没有结构。只需将其输出为字符串。

答案 2 :(得分:0)

除非您需要读取/使用自己的XML站点地图文件,否则只需像其他人一样输出到字符串。 XML站点地图格式非常简单。如果你打算支持子类型那么......那么我仍然会以字符串为基础。

答案 3 :(得分: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;
相关问题