RSS Reader

Post here is you are having problems with any of the tutorials.
Post Reply
wistex
Posts: 13
Joined: Fri Jun 17, 2011 10:07 pm

RSS Reader

Post 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\read­ing_rss_feeds\core\inc\bbc_new­s_reader.inc.php on line 15

I'm new enough to not understand this.

Thank you,

Jon
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: RSS Reader

Post by Temor »

Post your full code here using the PHP tags so that we can take a look at it.
wistex
Posts: 13
Joined: Fri Jun 17, 2011 10:07 pm

Re: RSS Reader

Post 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;
}

?>
wistex
Posts: 13
Joined: Fri Jun 17, 2011 10:07 pm

Re: RSS Reader

Post 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	
			}
			
			?>
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: RSS Reader

Post 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.
User avatar
Kamal
Posts: 123
Joined: Fri May 06, 2011 10:45 am
Contact:

Re: RSS Reader

Post by Kamal »

Simply because
$media->thumbnail[0]
does not exist, or you misspelled attributes() (maybe its a property not a method)
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: RSS Reader

Post 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.
Image
Post Reply