not able to add lastmod field to sitemap xml file with a php script properly



I have some sitemaps file for our site. which is in following format



<urlset>
<url>
<loc>
http://url1.com
</loc>
</url>
<url>
<loc>
http://url2.com
</loc>
</url>
.
.
</urlset>


I have 50000 urls in sitemap files in above format.I want to add lastmod tag next to loc tag in each fragment with the following code.



<?php
date_default_timezone_set('UTC');
$last1=date("Y-m-d");
for($i=1;$i<=4;$i++)
{
$xml = simplexml_load_file("sitemap$i.xml");
$sxe = new SimpleXMLElement($xml->asXML());
foreach($sxe->url as $array)
{

$array->addChild("lastmod", $last1);
$sxe->asXML("sitemap$i.xml");
}
}
?>


When i run this code as a cron job,it runs for sometime and appends lastmod tag for first file for some urls only. Lets say it adds lastmod tag only for 10000 url fragments for sitemap1.xml file and then it throws an error and script is stopped.Because of that first file is in screwed up format and other files are not altered and script is stopped in between..


Please see the code if anything wrong i m doing because of which code is stopping in between and giving me the error.


No comments:

Post a Comment