Show the rss description element on another page - php - php

I am parsing rss feed using a small program i have written. I want the user to see a small exerpt of the rss description element on the first page which i have done as below:
$rss = new DOMDocument();
$rss->load($rsslink);
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
for($x=0;$x<$limit;$x++) {
$randomnum= rand(5, 20);
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<b>'.$title.'</b><br />';
echo '<i>Posted on '.$date.'</i><br/><br/>';
$des = strip_tags($description);
echo $this-> get_words_until($des,70);
echo 'readmore';
echo '<br/>';
}
1.
2.
I want to achieve something like the second image on another php page when the user clicks on the read more link
Please can anyone help me?

Related

Trying to get property of a non object on rss feed script

I have the following script
public function NewsRss() {
$rss = new DOMDocument();
libxml_use_internal_errors(true);
$rss->load('http://www.autoexpress.co.uk/feeds/all');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$htmlStr = $node->getElementsByTagName('description')->item(0)->nodeValue;
$html = new DOMDocument();
$html->loadHTML($htmlStr);
$desc = $html->getElementsByTagName('p')->item(0)->nodeValue;
//var_dump($desc);
$imgTag = $html->getElementsByTagName('img');
$img = ($imgTag->length==0)?'noimg.png':$imgTag->item(0)->getAttribute('src');
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $desc,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
'image' => $img,
);
array_push($feed, $item);
}
$limit = 3;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<div class="news-row-index">';
echo '<div class="details-index"><p><h5>'.$title.'</h5>';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<div class="img"><img src="'.$feed[$x]['image'].'" height="79" width="89"></div>';
echo '<p>'.$description.'</p></div>';
echo '</div>';
}
echo '<a style="margin-left:10px;" class="view-all-but" target="_blank" href="http://www.autoexpress.co.uk/feeds/all">View all</a>';
}
The problem is that us showing me a notice like this
Notice: Trying to get property of non-object in on line 10 which is
$desc = $html->getElementsByTagName('p')->item(0)->nodeValue; but if I hide the errors everything works fine, just that i am trying to clean up my script of errors, and on this case don't know exactly where to start. I know that -> should not be used many times.
Please help me on this, it will be much appreciated. I am kind of a noob in OOP and rss.
It looks like there are no p tags found, so the call to getElementsByTagName returns an empty list, and then you try to call item(0) on an empty list, which is why you get the error.
You could split it over 2 lines to check for no elements:
$p = $html->getElementsByTagName("p");
if($p->length > 0) {
$desc = $p->item(0)->nodeValue;
} else {
$desc = "";
}

Grab rss feed with php and change value in rss string

Ok I have a site where I use this code to grab the stock quote for each stock searched. <?php echo $_GET['quote'];?> What i'm trying to do is to display RSS news data by using this code below:
<?php
$rss = new DOMDocument();
$rss->load('http://feeds.finance.yahoo.com/rss/2.0/headline?s=GOOG&region=US&lang=en-USsto');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong>'.$title.'</strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
?>
Do you see the "GOOG" section? that is what I'm trying to change dynamically with the quote capture code <?php echo $_GET['quote'];?> and it throws errors! Is there any other way to do this?
If your GET value contains a legitimate ticker code then this would work.
$rss->load('http://feeds.finance.yahoo.com/rss/2.0/headline?s='.$_GET['quote'].'&region=US&lang=en-USsto');
You are already in a php context so is not the way to concatenate a string
This is however not a robust way to handle it as there is no checking that $_GET['quote'] is set or has any value, you would need to decide what to get if it was not set
UPDATE
NB the original URL given in the question is invalid
http://feeds.finance.yahoo.com/rss/2.0/headline?s=GOOG&region=US&lang=en-USsto
does not work but
http://feeds.finance.yahoo.com/rss/2.0/headline?s=GOOG&region=US&lang=en-US
does
So please update your code to
$rss->load('http://feeds.finance.yahoo.com/rss/2.0/headline?s='.$_GET['quote'].'&region=US&lang=en-US');

php rss feed show images

I am trying to show the images in the following rss feed and would appreciate help from somebody please. I have tried getAttribute but am not sure how to format it or where in the code to put it, so I have taken it out to avoid confusion. The following code is working, but I need to add the code to display the images.
<?php
$rss = new DOMDocument();
$rss->load('http://xml.thinkspain.com/think-spain-feeds/spanish-news.xml');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong>'.$title.'</strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
?>
You can get attribute by using getAttribute :
$node->getElementsByTagName('enclosure')->item(0)->getAttribute('url')

Displaying Thumbnail from RSS Feed

I am currently displaying an RSS feed and I am grabbing the title, link and date for each article. I would also like to grab the media:thumbnail for each article. Here is the PHP code I am using to retrieve the RSS feed.
<?php
$rss = new DOMDocument();
$rss->load('http://www.avfc.co.uk/rss/ptv/page/CustomArticleIndex/0,,10265~2282152,00.xml');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 2;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<div id="feed"><a target="_blank" href="'.$link.'" title="'.$title.' ">'.$title.'</a><br />';
echo '<a class="date">Posted on '.$date.'</a></div>';
}
?>
The RSS feed I am displaying is: http://www.avfc.co.uk/rss/ptv/page/CustomArticleIndex/0,,10265~2282152,00.xml
I'm not that good with PHP so does anybody know how I can go about doing this? Thanks.
Try that:
$thumb = $node->getElementsByTagName('media:thumbnail')->item(0)->attributes->getNamedItem('url')->nodeValue;

Time since pubDate

In a new project, I'm working with rss being read by PHP and displayed on a page.
One thing I'd like to do is show how much time has passed since the post was published, but I can't find a way to do so, this is my current code, hope somebody can help me!
echo "<div id=\"left\">";
$rss1 = new DOMDocument();
$rss1->load('http://www.macfan.nl/macfan.rss');
echo '<h2>MacFan</h2>';
$feed = array();
foreach ($rss1->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$date = date('F d', strtotime($feed[$x]['date']));
echo '<p>'.$title.'</p>';
echo "<p class=\"small\">$date</p>";
}
echo "</div>";
You can compare the unix timestamps;
$seconds_between_now_and_then=(time()-strtotime($feed[$x]['date']));
Then you can see how far it is apart. These below could help you make it more readable for yourself:
$minutes_between_now_and_then=$seconds_between_now_and_then/60;
$hours_between_now_and_then=$minutes_between_now_and_then/60;
$days_between_now_and_then=$minutes_between_now_and_then/24;
echo 'Seconds:'.$seconds_between_now_and_then.'<br />';
echo 'Minutes:'.$minutes_between_now_and_then.'<br />';
echo 'Hours:'.$hours_between_now_and_then.'<br />';
echo 'Days:'.$days_between_now_and_then;

Categories