Reading RSS Feeds

Post here is you are having problems with any of the tutorials.
Post Reply
pad1989
Posts: 1
Joined: Wed Nov 30, 2011 1:45 am

Reading RSS Feeds

Post by pad1989 »

After follow the part 2 Tutorial on this I keep getting the following error
Fatal error: Call to a member function attributes() on a non-object in E:\College 2011\USBWebserver v8_en\root\RSS FEED\core\inc\bbc_news_reader.inc.php on line 16]
.

I have followed the code exactly.

My code is
<?php

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,
		);
	}
	print_r($articles);
}
?>

If anyone can help in any way I would be very greatful.

Thanks
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Reading RSS Feeds

Post by jacek »

I think this might happen if an article has no thumbnail, try printing out $media
print_r($media);
And see if you can see any that do not have a thumbnail set.

If this is the problem, the solution would be to only look for an image when there is one
if (empty($media->thumbnails) === false){
    foreach ($media->thumbnail[0]->attributes() as $key => $value){
        $image[$key] = (string)$value;
    }
}
Something like that anyway.
Image
jkmetko
Posts: 1
Joined: Sun Dec 18, 2011 11:51 am

Re: Reading RSS Feeds

Post by jkmetko »

First of all I want to thank you for posting this video tutorial.

As you explained $media->thumbnail[0]->attributes() gets smaller thumbnail and $media->thumbnail[1]->attributes() gets the bigger one.

There's problem getting the large one. When I use:
if (empty($media->thumbnails) === false){
    foreach ($media->thumbnail[1]->attributes() as $key => $value){
         $image[$key] = (string)$value;
     }
}
the $article['image']['url'] is empty. Can you help me with this problem?
Thanks.
Last edited by jacek on Sun Dec 18, 2011 3:45 pm, edited 1 time in total.
Reason: code tags
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Reading RSS Feeds

Post by jacek »

It might just be that some articles only have the small thumbnail ?

Or does this happen for all articles ?
Image
slnglm
Posts: 1
Joined: Sat Jan 07, 2012 4:34 pm

Re: Reading RSS Feeds

Post by slnglm »

Hello,

Im having the same problem, I am using the same BBC feed url, and also i printed out the attributes and it seems like the items and thumbnails are not empty. I also checked with the " if (empty($media->thumbnails) === false) " statement too. My problem is the foreach loop does not stop until it gets the "fatal error". How can I fix this ?

Thank you,
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Reading RSS Feeds

Post by jacek »

What does
print_r($media->thumbnail);
show if you use it just before the loop ?
Image
Post Reply