Georgi.Bonchev Georgi.Bonchev - 6 months ago 32
PHP Question

PHP reading RSS feed gets error on the third link in a node

I am reading a RSS feed and each node has 3 links:



<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=''/>





I read the "href" attribute with this:



'link' => $node->getElementsByTagName('link')->item(0)->getAttribute('href')





There is no problem when I use item(0) for the first link, there is no problem when I use item(1) for the second link but when I use item(2) for the third link I get this error:

Fatal error: Call to a member function getAttribute() on a non-object

Any idea how can I solve it?

Here is my full code:

<?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(' & ', ' &amp; ', $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>';
}


?>

Answer

EDIT After looking at the URL you provided I made adjustments to the code using DOMXPath, like this:

$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";
}

The key here is to register the default namespace in order for the code to work.

Comments