Page 1 of 1
RSS Reader
Posted: Thu Aug 18, 2011 11:10 pm
by wistex
Jacek
Thank you for the great tutorials. I want you to know I learn so much from you. I followed along with the RSS Reader tutorial today and somewhere during the second video things went wrong. Here is the message I'm receiving.
Fatal error: Call to a member function attributes() on a non-object in C:\xampp\htdocs\workspace\reading_rss_feeds\core\inc\bbc_news_reader.inc.php on line 15
I'm new enough to not understand this.
Thank you,
Jon
Re: RSS Reader
Posted: Fri Aug 19, 2011 1:00 am
by Temor
Post your full code here using the PHP tags so that we can take a look at it.
Re: RSS Reader
Posted: Fri Aug 19, 2011 12:18 pm
by wistex
<?php
// fetches articles from the BBC news RSS feed
function fetch_news(){
$data = file_get_contents('http://feeds.bbci.co.uk/news/rss.xml');
$data = simplexml_load_string($data);
$articles = array();
foreach($data->channel->item as $item){
$media = $item->children('http://search.yahoo.com/mrss/');
$image = array();
foreach ($media->thumbnail[0]->attributes() as $key => $value){
$image[$key] = (string)$value;
}
$articles[] = array(
'title' => (string)$item->title,
'description' => (string)$item->description,
'link' => (string)$item->link,
'date' => (string)$item->pubDate,
'image' => $image
);
}
return $articles;
}
?>
Re: RSS Reader
Posted: Fri Aug 19, 2011 12:18 pm
by wistex
<?php
foreach (fetch_news() as $article) {
?>
<h3><a href="<?php echo $article['link'] ?>"><?php echo $article['title']; ?></a></h3>
<img src="<?php echo $article['image']['url']; ?>" alt="" />
<p>
<?php echo $article['description']; ?>
</p>
<?php
}
?>
Re: RSS Reader
Posted: Fri Aug 19, 2011 7:24 pm
by Temor
what version of php are you running?
I cannot spot the problem right away, so it might be that you have an outdated version of php.
do this:
<?php
phpinfo();
?>
to get your php version.
Re: RSS Reader
Posted: Fri Aug 19, 2011 9:22 pm
by Kamal
Simply because
$media->thumbnail[0]
does not exist, or you misspelled attributes() (maybe its a property not a method)
Re: RSS Reader
Posted: Sat Aug 20, 2011 12:15 pm
by jacek
Before the innermost foreach loop you could add print_r($media) to see what info you have in the media namespace.
It's possible that not all articles have images and I was just lucky in the video.