Page 1 of 1

Reading RSS Feeds

Posted: Wed Nov 30, 2011 1:53 am
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

Re: Reading RSS Feeds

Posted: Wed Nov 30, 2011 5:08 pm
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.

Re: Reading RSS Feeds

Posted: Sun Dec 18, 2011 11:56 am
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.

Re: Reading RSS Feeds

Posted: Sun Dec 18, 2011 3:46 pm
by jacek
It might just be that some articles only have the small thumbnail ?

Or does this happen for all articles ?

Re: Reading RSS Feeds

Posted: Sat Jan 07, 2012 4:38 pm
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,

Re: Reading RSS Feeds

Posted: Sun Jan 08, 2012 12:48 am
by jacek
What does
print_r($media->thumbnail);
show if you use it just before the loop ?