PHP and sitemap.xml

China☆狼群 提交于 2019-12-25 14:14:54

问题


I am planning to build a script that will create a sitemap.xml for my site, say, every day (cron will execute the script). Should I just build the XML string and save it as a file? Or would there be some benefit to using one of PHP's classes/functions/etc. for XML?

If I should be using some sort of PHP class/function/etc., what should it be?


回答1:


For simple XML it is often easier to just output the string. But the more complex your document gets, the more benefit you will get from using an XML library (either those included with PHP or a third party script) as it will help you to output correct XML.

For a sitemap, you would probably be best just writing the string.




回答2:


It's simple format. Almost no structure. Just output it as string.




回答3:


Unless you need to read/consume your own XML sitemap files, just output to string like others said. The XML sitemaps format is fairly simple. If you intend to support the subtypes as well then... Well I would still do it string based.




回答4:


I would suggest you to do cron to put all of the url in an array and store it as a cache. Then you could use this Kohana module to generate the sitemap.xml on the fly.

    // 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;


来源:https://stackoverflow.com/questions/4981294/php-and-sitemap-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!