我正在阅读RSS源,每个节点都有3个链接:
<link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2202110476673931679/6339893542751280730/comments/default/1280042367141045524'/>
<link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2202110476673931679/6339893542751280730/comments/default/1280042367141045524'/>
<link rel='alternate' type='text/html' href='http://misterika.blogspot.com/2016/04/blog-post_11.html?showComment=1460801110852#c1280042367141045524' title=''/>
我用这个读了“href”属性:
'link' => $node->getElementsByTagName('link')->item(0)->getAttribute('href')
当我将项目(0)用于第一个链接时没有问题,当我将项目(1)用于第二个链接时没有问题但是当我将项目(2)用于第三个链接时我得到此错误:
致命错误:在非对象
上调用成员函数getAttribute()我知道如何解决它?
这是我的完整代码:
<?php
$rss = new DOMDocument();
$rss->load('http://misterika.blogspot.com/feeds/comments/default');
$feed = array();
foreach ($rss->getElementsByTagName('entry') as $node) {
$item = array (
'title' => $node->getElementsByTagName('name')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('content')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(2)->getAttribute('href'),
'date' => $node->getElementsByTagName('published')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$link.'</p>';
echo '<p>'.$description.'</p>';
}
&GT;
答案 0 :(得分:1)
当我使用以下示例代码段进行测试时,它正在工作。
<?php
$xml = "<root><entry><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2202110476673931679/6339893542751280730/comments/default/1280042367141045524'/>
<link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2202110476673931679/6339893542751280730/comments/default/1280042367141045524'/>
<link rel='alternate' type='text/html' href='http://misterika.blogspot.com/2016/04/blog-post_11.html?showComment=1460801110852#c1280042367141045524' title=''/></entry>
<entry><link rel='edit' type='application/atom+xml' href='http://google.com/'/>
<link rel='self' type='application/atom+xml' href='http://jenson.in/'/></entry></root>";
$node = new DOMDocument;
$node->loadXML($xml);
foreach($node->getElementsByTagName("entry") as $entry)
{
$link = $entry->getElementsByTagName("link");
echo $node->getElementsByTagName('link')->item(0)->getAttribute('href')."<br/>";
echo $node->getElementsByTagName('link')->item(1)->getAttribute('href')."<br/>";
//Below code checks if third link exists or not.
echo ($link->length > 2)?$node->getElementsByTagName('link')->item(2)->getAttribute('href'):"No alternate link!"."<br/>";
}
?>
<强> See Demo 强>
<强>更新强>
在Feed XML中,http://misterika.blogspot.com/2016/03/blog-post_20.html?showComment=1462627509971#c2966841279736454385
entry
节点中只有2个链接可用后没有第3个链接。这就是你收到错误的原因。
答案 1 :(得分:0)
修改强> 在查看您提供的网址后,我使用DOMXPath对代码进行了调整,如下所示:
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
$rss = file_get_contents('http://misterika.blogspot.com/feeds/comments/default');
$doc->loadXML($rss);
$xpath = new DOMXpath($doc);
$xpath->registerNameSpace('atom', 'http://www.w3.org/2005/Atom');
$links = $xpath->query('/atom:feed/atom:entry/atom:link[@href]');
foreach ($links as $link) {
$node = $link->nodeName;
$href = $link->getAttribute('href');
echo "{$node} - {$href}\n";
}
这里的关键是注册默认命名空间,以使代码正常工作。