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
RSS Reader
Re: RSS Reader
Post your full code here using the PHP tags so that we can take a look at it.
Re: RSS Reader
[syntax=php]<?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;
}
?>[/syntax]
// 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;
}
?>[/syntax]
Re: RSS Reader
[syntax=php] <?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
}
?>[/syntax]
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
}
?>[/syntax]
Re: RSS Reader
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:
[syntax=php]<?php
phpinfo();
?>[/syntax]
to get your php version.
I cannot spot the problem right away, so it might be that you have an outdated version of php.
do this:
[syntax=php]<?php
phpinfo();
?>[/syntax]
to get your php version.
Re: RSS Reader
Simply because
[syntax=php]$media->thumbnail[0][/syntax]
does not exist, or you misspelled attributes() (maybe its a property not a method)
[syntax=php]$media->thumbnail[0][/syntax]
does not exist, or you misspelled attributes() (maybe its a property not a method)
Re: RSS Reader
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.
It's possible that not all articles have images and I was just lucky in the video.