2011-08-03 11 views
1

DOMを使用した自分のツイッターフィードの総数をどのように数えることができますか?DOMを使った私のツイッターフィードの総数をどのように数えることができますか?

私のフィードが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 
} 
?> 

答えて

2
$xpath = new DOMXPath($xmlDoc); 
$count = $xpath->evaluate('count(//item)'); 
+0

おかげでそんなに! – laukok

関連する問題