Php 致命错误:对非对象调用成员函数attributes()
我正在尝试从Blogger提要创建我的XML站点地图,所以我用下面的代码进行了尝试,然后工作正常Php 致命错误:对非对象调用成员函数attributes(),php,xml,rss,feed,sitemap,Php,Xml,Rss,Feed,Sitemap,我正在尝试从Blogger提要创建我的XML站点地图,所以我用下面的代码进行了尝试,然后工作正常 <?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"
<?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>';
?>
但当我将“max results=368”增加到“max results=369”时,我得到了一个错误,即“文档末尾的额外内容”,在XML页面的末尾,它显示:
(!)致命错误:对非对象调用成员函数attributes()
现在该怎么办?因为这是动态的,所以在调用它之前需要检查它是否存在:
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>';
?>
foreach($sitemapONE->entry as$value)
{
如果(设置($value->link[4]))
{
回声'
http://www.exeideas.com/“.$value->link[4]->attributes()->href。”
“.$value->已更新。”
总是
';
}
}
回声';
?>
谢谢用户2191572,您解决了我的问题。现在,对于新来者,我在这里附上完美的代码,通过php制作博客网站地图,该网站也经过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>';
?>
其简单的$value->link[4]不是对象,也没有名为attributes()的方法;在该行之前,您可以使用var_dump($values);检查您正在使用的数据。@Rottingham感谢您的评论,但user2191572做得很好。请看下面。这不会有帮助,因为您仍然在链接[4]
上调用属性()
,您需要检查链接[4]
是否是您期望的对象。@fdl正确,我没有注意到属性()
@user2191572的括号是您做的。。。谢谢…:-)