【发布时间】:2015-09-16 08:18:55
【问题描述】:
我正在尝试从 RSS 提要中获取图像。这是提要的结构:
<item>
<title>Title Here....</title>
<link>Link Here....</link>
<description>Description Here...</description>
<comments>Comments Here.</comments>
<pubDate>Tue, 15 Sep 2015 22:54:00 +0000</pubDate>
<guid isPermaLink="false">59551 at http://winsupersite.com</guid>
<media:thumbnail url="http://winsupersite.com/site-files/winsupersite.com/files/imagecache/thumb_img/uploads/2015/09/windowsrt81update3hero.png" />
<media:content url="http://winsupersite.com/site-files/winsupersite.com/files/uploads/2015/09/windowsrt81update3hero.png" medium="image">
</media:content>
</item>
这是我的 PHP 代码:
$rss = new DOMDocument();
$rss->load('http://winsupersite.com/rss.xml');
foreach ($rss->getElementsByTagName('item') as $node) {
$title = $node->getElementsByTagName('title')->item(0)->nodeValue;
$src = $node->getElementsByTagName('thumbnail')->item(0)->getAttribute('url');
$link = $node->getElementsByTagName('link')->item(0)->nodeValue;
}
但我得到了错误:
在非对象上调用成员函数 getAttribute()
为什么会出现此错误? thumbnail 标签和url 属性都存在。
这是 $rss 上 var_dump 的结果:
object(DOMDocument)#1 (34) {
["doctype"]=> NULL ["implementation"]=> string(22) "(object value omitted)"
["documentElement"]=> string(22) "(object value omitted)" ["actualEncoding"]=> string(5) "utf-8"
["encoding"]=> string(5) "utf-8" ["xmlEncoding"]=> string(5) "utf-8" ["standalone"]=> bool(true)
["xmlStandalone"]=> bool(true) ["version"]=> string(3) "1.0" ["xmlVersion"]=> string(3) "1.0" ["strictErrorChecking"]=> bool(true) ["documentURI"]=> string(31) "http://winsupersite.com/rss.xml"
["config"]=> NULL ["formatOutput"]=> bool(false) ["validateOnParse"]=> bool(false)
["resolveExternals"]=> bool(false) ["preserveWhiteSpace"]=> bool(true) ["recover"]=> bool(false) ["substituteEntities"]=> bool(false) ["nodeName"]=> string(9) "#document" ["nodeValue"]=> NULL ["nodeType"]=> int(9) ["parentNode"]=> NULL
["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> NULL ["attributes"]=> NULL
["ownerDocument"]=> NULL ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> NULL ["baseURI"]=> string(31) "http://winsupersite.com/rss.xml" ["textContent"]=> string(27278) " SuperSite for Win........ Just text after this
【问题讨论】:
-
那么是
$rss是什么?我们无法猜测,您必须告诉我们。将变量转储到日志文件中并查看。很可能创建对象只是失败了。你真的应该开始查看你的日志文件。您可以自己查看问题所在。 -
$rss 是一个变量。这是我从一开始的脚本,没有别的了。
-
我问
$rss的内容是什么。倾倒它。见。 -
@arkascha 我已经更新了这个问题。
-
@SujataHulsurkar 检查下面的代码它将解决您的问题。