Grab rss feed with php and change value in rss string - php

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');

Related

Show the rss description element on another page - 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?

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;

PHP ReadFile Insert a code in readfile

I have a php readfile script, like this:
<?php
$contentFile = "http://google.com";
readfile( $contentFile );
?>`
I want to insert a code in a specific line in the output of the readfile.
Example:
<html>
{top_code}
{Code i want to insert here}
{bottom-code}
</html>
How can I make this possible?
You can't. readfile() streams whatever you're reading out to the user's browser. You could use the output buffering mechanism to capture that data instead, but then you might as well just use file_get_contents() instead and save yourself a few extra lines of code.
file_get_contents returns the requested file/url as a string. Then you use standard string or DOM operations to manipulate that 'page'.
This can do the job
$contentFile = "http://google.com";
$html = file_get_contents($contentFile);
$html = explode("\n",$html);
$line = $line_number - 1;
array_splice($html, $line, 0,"Burim Shala");
$html = implode("\n",$html);
I found this, to be the solution to the problem of RSS feeds using PHP without MYSQL
Thanks to http://bavotasan.com/2010/display-rss-feed-with-php/
$rss = new DOMDocument();
$rss->load('http://wordpress.org/news/feed/');
$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>';
}

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