php rss feed show images - php

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

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?

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

RSS affiliate iTunes php parser

I tried to create a PHP script in order to extract all the data but this is not working. I tried to get the im:price and other attributes but I don't know how to get them.
Can anyone help me correct my script?
<?php
$rss = new DOMDocument();
$rss->load('https://itunes.apple.com/fr/rss/topfreeapplications/limit=25/xml');
$feed = array();
foreach ($rss->getElementsByTagName('entry') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'price' => $node->getElementsByTagName('im')->item(0)->getAttribute('price')
'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> Price is '.$price.'</p>';
}
?>
In order to parse all of the im nodes you have to use namespaces like so.
$namespaces = $entry->getDocNamespaces(true);
$items = $entry->children($namespaces['im']);
Here is a sample to get you started:
<?php
$rss = "https://itunes.apple.com/fr/rss/topfreeapplications/limit=25/xml";
$xml = simplexml_load_file($rss);
foreach ($xml->entry as $entry){
$namespaces = $entry->getDocNamespaces(true);
$items = $entry->children($namespaces['im']);
$name = $items->name;
$summary = $entry->summary;
$price = $items->price;
$image = $items->image[1];
$rights = $entry->rights;
For all of the im nodes use $items->foo;. For all of the nodes without the im namespace use $entry->bar;

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