Hi,
I have two problems in this atom reader:
how can I get the total items of this feed which is <entry>
node?
how can I get the href from the <link>
node?
<?php
/**
* @integer @limit | default:1
*/
function atom_reader($url,$limit = 1,$title = null)
{
$xml = ($url); //http://twitter.com/statuses/friends_timeline/321998072.rss
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
# Count the total feed with xpath.
$xpath = new DOMXPath($xmlDoc);
$total_feed = $xpath->evaluate('count(entry)');
//return $total_feed;
# Set feed limit.
$limit_feed = $limit;
# Check if the total feed is less than the limit then use the total feed as the limit.
if($limit_feed >= $total_feed) $limit_feed = $total_feed;
# Set the variable.
$output = '';
# get and output "<item>" elements
$x = $xmlDoc -> getElementsByTagName('entry');
for ($i=0; $i<2; $i++)
{
$item_title = $x->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
//$item_link = $x->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
$item_date = $x->item($i)->getElementsByTagName('updated')->item(0)->childNodes->item(0)->nodeValue;
$item_content = $x->item($i)->getElementsByTagName('content')->item(0)->childNodes->item(0)->nodeValue;
# NOTE: use this code for the server runs PHP5.3
# DateTime::add — Adds an amount of days, months, years, hours, minutes and seconds to a DateTime object
$date = new DateTime($item_date);
# change the date format into Y-m-d H:i:s
$item_date = $date->format('j F Y');
# count time ago from the published date
//$time_ago = time_ago($date->format('Y-m-d H:i:s'),'d M Y \\a\ H:i');
$time_ago = $date->format('j F Y');
$output .= '<li><p>'.strip_tags($item_content).'<br/>'.$time_ago.'</p></li>';
}
return $output;
}
echo atom_reader($url = 'http://www.globaltoleranceconnect.com/profiles/blog/feed?xn_auth=no',$limit = 4);
?>
the atom feed I get is like this,
<feed xmlns="http://www.w3.org/2005/Atom">
<id>http://localhost:8080/resources/timezones</id>
<title type="text">Time Zones</title>
<link href="http://localhost:8080/resources/timezones" rel="self">
</link>
<author>
<name>sandra</name>
</author>
<entry>
<id>http://localhost:8080/resources/timezones/Etc_GMT+12</id>
<title type="text">Etc/GMT+12 :: GMT-12:00</title>
<link href="http://localhost:8080/resources/timezones/Etc_GMT+12" rel="self">
</link>
<author>
<name>No Author</name>
</author>
<updated>1970-01-01T00:00:00.000Z</updated>
<content type="text">Oct 23, 2008 4:12:37 AM</content>
</entry>
<entry>
<id>http://localhost:8080/resources/timezones/Etc_GMT+11</id>
<title type="text">Etc/GMT+11 :: GMT-11:00</title>
<link href="http://localhost:8080/resources/timezones/Etc_GMT+11" rel="self">
</link>
<updated>1970-01-01T00:00:00.000Z</updated>
<content type="text">Oct 23, 2008 5:12:37 AM</content>
</entry>
<entry>
<id>http://localhost:8080/resources/timezones/MIT</id>
<title type="text">MIT :: West Samoa Time</title>
<link href="http://localhost:8080/resources/timezones/MIT" rel="self">
</link>
<updated>1970-01-01T00:00:00.000Z</updated>
<content type="text">Oct 23, 2008 5:12:37 AM</content>
</entry>
.
.
.
</feed>
this does not return the total items,
$xpath = new DOMXPath($xmlDoc);
$total_feed = $xpath->evaluate('count(entry)');
and this does not get the href of the <link>,
$item_link = $x->item($i)->getElementsByTagName('link')->item(0)->attribute;
any idea?
Thanks,
Lau