Question

I am trying to make my XML sitemap from Blogger feed so I tried it with the below code then its working fine.

<?php
header('application/rss+xml; charset=utf-8');
header('Content-Type: text/xml');
header('Pragma: public');
header('Cache-control: private');
header('Expires: -1');
echo '<?xml version="1.0" encoding="utf-8"?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
';
$sitemapONE = simplexml_load_file('http://www.exeideas.com/atom.xml?redirect=false&start-index=1&max-results=368');
foreach($sitemapONE->entry as $value)
{
echo '
<url>
<loc>http://www.exeideas.com/'.$value->link[4]->attributes()->href.'</loc>
<lastmod>'.$value->updated.'</lastmod>
<changefreq>always</changefreq>
</url>
';
}
echo '</urlset>';
?>

But when I increased my "max-results=368" To "max-results=369", I got an error as "Extra content at the end of the document" And in the end of XML page, it shows:

( ! ) Fatal error: Call to a member function attributes() on a non-object.............

What to do now?

Was it helpful?

Solution

Since this is dynamic, you need to check for its' existence before calling it:

foreach($sitemapONE->entry as $value)
{
    if(isset($value->link[4]))
    {
        echo '
        <url>
        <loc>http://www.exeideas.com/'.$value->link[4]->attributes()->href.'</loc>
        <lastmod>'.$value->updated.'</lastmod>
        <changefreq>always</changefreq>
        </url>
        ';
    }
}
echo '</urlset>';
?>

OTHER TIPS

Thanks user2191572, You solved my problem. Now for new comers, Here I am attaching perfect code to make your Blogger sitemap via php that is also validated by validator.w3.org...

<?php
header('application/rss+xml; charset=utf-8');
header('Content-Type: text/xml');
header('Pragma: public');
header('Cache-control: private');
header('Expires: -1');
echo '<?xml version="1.0" encoding="utf-8"?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
';
$sitemapONE = simplexml_load_file('http://www.exeideas.com/atom.xml?redirect=false&start-index=1&max-results=500');
foreach($sitemapONE->entry as $value)
{
echo '
<url>
<loc>http://www.exeideas.com/'.$value->link[4]->attributes()->href.'</loc>
<lastmod>'.$value->updated.'</lastmod>
<changefreq>always</changefreq>
</url>
';
}
echo '</urlset>';
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top