如何使用 DOM 计算我的 twitter feed 中的项目总数?
如何使用 DOM 计算我的 twitter feed 中的项目总数?
当我的 feed 少于 3 时,下面的代码将导致错误 - 我该如何修复它?
<?php
$xml = ("http://twitter.com/statuses/user_timeline/321998072.rss"); //http://twitter.com/statuses/friends_timeline/321998072.rss
$xmlDoc = new DOMDocument();
$xmlDoc -> load($xml);
# get elements from "<channel>"
$channel = $xmlDoc -> getElementsByTagName('channel') -> item(0);
$channel_title = $channel -> getElementsByTagName('title') -> item(0) -> childNodes -> item(0) -> nodeValue;
$channel_link = $channel -> getElementsByTagName('link') -> item(0) -> childNodes -> item(0) -> nodeValue;
$channel_desc = $channel -> getElementsByTagName('description') -> item(0) -> childNodes -> item(0) -> nodeValue;
# output elements from "<channel>"
/*
echo("<p><a href='" . $channel_link
. "'>" . $channel_title . "</a>");
echo("<br />");
echo($channel_desc . "</p>");
*/
?>
<h4 class="heading-24dot">Twitter Updates</h4>
<p class="item-twitter channel-title"><a href="<?php echo $channel_link;?>" target="_blank" class="hover-opacity-04"><?php echo $channel_title;?></a></p>
<?php
# get and output "<item>" elements
$x = $xmlDoc -> getElementsByTagName('item');
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('pubDate') -> item(0) -> childNodes -> item(0) -> nodeValue;
$item_desc = $x -> item($i) -> getElementsByTagName('description') -> 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('Y-m-d H:i:s');
# count time ago from the published date
$time_ago = time_ago($date -> format('Y-m-d H:i:s'),'d M Y \a\t H:i');
?>
<p class="item-twitter"><a href="<?php echo $item_link;?>" target="_blank" class="hover-opacity-06"><?php echo preg_replace('/^(GingerMonkey_TL:)/', ' ',$item_desc);?></a><br/><span class="date-twitted-ago"><?php echo $time_ago;?></span></p>
<?php
}
?>
How can I count the total number items from my twitter feed with DOM?
This code below will cause an error when my feed is less than 3 - how can I fix it??
<?php
$xml = ("http://twitter.com/statuses/user_timeline/321998072.rss"); //http://twitter.com/statuses/friends_timeline/321998072.rss
$xmlDoc = new DOMDocument();
$xmlDoc -> load($xml);
# get elements from "<channel>"
$channel = $xmlDoc -> getElementsByTagName('channel') -> item(0);
$channel_title = $channel -> getElementsByTagName('title') -> item(0) -> childNodes -> item(0) -> nodeValue;
$channel_link = $channel -> getElementsByTagName('link') -> item(0) -> childNodes -> item(0) -> nodeValue;
$channel_desc = $channel -> getElementsByTagName('description') -> item(0) -> childNodes -> item(0) -> nodeValue;
# output elements from "<channel>"
/*
echo("<p><a href='" . $channel_link
. "'>" . $channel_title . "</a>");
echo("<br />");
echo($channel_desc . "</p>");
*/
?>
<h4 class="heading-24dot">Twitter Updates</h4>
<p class="item-twitter channel-title"><a href="<?php echo $channel_link;?>" target="_blank" class="hover-opacity-04"><?php echo $channel_title;?></a></p>
<?php
# get and output "<item>" elements
$x = $xmlDoc -> getElementsByTagName('item');
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('pubDate') -> item(0) -> childNodes -> item(0) -> nodeValue;
$item_desc = $x -> item($i) -> getElementsByTagName('description') -> 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('Y-m-d H:i:s');
# count time ago from the published date
$time_ago = time_ago($date -> format('Y-m-d H:i:s'),'d M Y \a\t H:i');
?>
<p class="item-twitter"><a href="<?php echo $item_link;?>" target="_blank" class="hover-opacity-06"><?php echo preg_replace('/^(GingerMonkey_TL:)/', ' ',$item_desc);?></a><br/><span class="date-twitted-ago"><?php echo $time_ago;?></span></p>
<?php
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)