RSS feed limit items - php

How can i limit the items from my RSS-feed? (Example: 5 items). I hope someone can help me with this code.
<?php
$url = "LINK TO RSS";
$rss = simplexml_load_file($url);
if($rss)
{
$items = $rss->channel->item;
foreach($items as $item)
{
$title = $item->title;
$link = $item->link;
$published_on = $item->pubDate;
$description = $item->description;
echo '<p class="rss-feed">'.$title.'</p>';
echo '<p>'.$description.'</p>';
}
}
?>

If you want to limit the number of items displayed you can use the array_slice function to store a subset of the retrieved values in $items, e.g. change:
$items = $rss->channel->item;
to:
$items = array_slice($rss->channel->item, 0, 5);
If you want to change the number of items fetched from the URL then you need to check whether/how this URL supports this behaviour.

Related

Displaying XML feed content

I am trying to get the contents from an xml feed and display them in a list. My test below is just to get the job_title for a vacancy.
$feed = file_get_contents('https://www.jobs.nhs.uk/search_xml?client_id=120650');
$xml = simplexml_load_string($feed);
$items = $xml->nhs_search->vacancy_details;
foreach($items as $item) {
$job_title = $item->job_title;
echo $job_title;
}
Here is a snippet of the xml feed
<nhs_search>
<vacancy_details>
<id>915854585</id>
<job_title>Band 5 Speech and Language Therapist</job_title>
</vacancy_details>
</nhs_search>
Nothing is displaying and no errors.
It's work fine:
$feed = file_get_contents('https://www.jobs.nhs.uk/search_xml?client_id=120650');
$xml = simplexml_load_string($feed);
$items = $xml->vacancy_details;
foreach ($items as $item) {
$id = $item->id;
$job_title = $item->job_title;
echo $id; echo '<br />';
echo $job_title;
}
Changed $items = $xml->nhs_search->vacancy_details; to $items = $xml->vacancy_details;

Get enclosure img url from rss feed

I have a problem with an rss feed in php. I want do get the img-url from "enclosure" but it´s not working.
My code just now:
$rss = simplexml_load_file($url);
$i = 0;
if($rss)
{
$items = $rss->channel->item;
foreach($items as $item)
{
$title = $item->title;
$link = $item->link;
$published_on = $item->pubDate;
$phpDate = strtotime($published_on);
$enclosure = $item['enclosure'][0]['url'];
From the RSS:
<enclosure url="http://www.svenskafans.com/image/7/141433/Snalla-Pelle-stanna-i-Gefle.jpg" lenght="51265" type="image/jpeg" />
Important to note is that sometimes there is not enclosure-tag with so it must work even if it is missing.
Thanks!
Best Regards
Charles
What about :
$rss=simplexml_load_file('http://www.svenskafans.com/rss/team/77.aspx');
foreach ($rss->channel->item as $item) {
if (isset($item->enclosure)) {
echo $item->enclosure['url'].'<br>';
}
}
outputs :
http://www.svenskafans.com/image/7/393988/Bilder-fran-tifot-for-Hugo-och-Bernhard.jpg
http://www.svenskafans.com/image/7/141433/Snalla-Pelle-stanna-i-Gefle.jpg
http://www.svenskafans.com/image/7/392527/Efter-Gefle-Elfsborg-En-skitmatch-i-regnet-gav-5-insikter.jpg
http://www.svenskafans.com/image/7/363552/Infor-Gefle-IF-IF-Elfsborg.jpg
http://www.svenskafans.com/image/7/211783/Gefles-Silly-Season-2013-2014-Angekeepern-Lloyd-Saxton-provtranar-med-Gefle.jpg
http://www.svenskafans.com/image/7/363058/Gefle-Panelen-17-Pensionera-Hugos-och-Bernhards-trojnummer.jpg
http://www.svenskafans.com/image/7/328214/Kungsbacksv-24-17-Hoppas-Hugo-satter-en-straff-mot-Elfsborg-i-89e-minuten.jpg
http://www.svenskafans.com/image/7/192682/Intervju-med-Daniel-Bernhardsson-Gefle-har-en-ljus-framtid.jpg
http://www.svenskafans.com/image/7/74875/Besked-idag-Bade-Bernhard-och-Hugo-spelar-sin-sista-match-i-Gefle-IF-pa-sondag.jpg
http://www.svenskafans.com/image/7/343968/Overraskande-piggt-Gefle-nar-Oremo-och-Jawo-natade.jpg
http://www.svenskafans.com/image/7/330399/Tack-AIK-nu-klart-till-100-att-Gefle-spelar-i-Allsvenskan-2014.jpg
http://www.svenskafans.com/image/7/363552/Rosta-fram-Gefles-MVP-2013.jpg
http://www.svenskafans.com/image/7/220468/Par-Asp-berattar-om-tiden-i-Gefle-roligaste-matchen-och-om-att-spela-med-Guidetti.jpg

How to turn variable feed to array?

I want to turn the variable into an array so I can store more than one feed?
<?php
error_reporting(0);
$feed_lifehacker_full = simplexml_load_file('http://feeds.gawker.com/lifehacker/full');
$xml = $feed_lifehacker_full;
//print_r($xml);
foreach ($xml->channel->item as $node){
$title = $node->title;
$link = $node->link;
$link = explode('/', $link);
$link = $link[8];
$url = $node->url;
$description = $node->description;
$pubDate = $node->pubDate;
preg_match_all('#(http://img[^\s]+(?=\.(jpe?g|png|gif)))#i', $description[0], $images);
$images = $images[0][1] . '.jpg';
if($images == '.jpg'){
//uncomment to show youtube articles
//$images = "http://placehold.it/640x360";
//echo "<a href='page2.php?a=$link' title='$title'><img src='$images' /></a><br>";
} else {
//article image
$images . '<br>';
echo "<a href='page2.php?a=$link' title='$title'><img src='$images' /></a><br>";
}
}
How can I change this to load to arrays,
$feed_lifehacker_full = simplexml_load_file('http://feeds.gawker.com/lifehacker/full');
$xml = $feed_lifehacker_full;
The script is just gathering the image of an rss feed and linking to a page, if you see how it can be done more efficiently feel free to say
it is possible to encode the result given as json and by decoding it it will return you an array
$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$array = json_decode($json, TRUE);

PHP foreach with two 'as'?

Hi there i am trying to combine two loops of foreach but i have a problem.
The problem is that the <a href='$link'> is same to all results but they must be different.
Here is the code that i am using:
<?php
$feed = file_get_contents('http://grabo.bg/rss/?city=&affid=16090');
$rss = simplexml_load_string($feed);
$doc = new DOMDocument();
#$doc->loadHTML($feed);
$tags = $doc->getElementsByTagName('link');
foreach ($tags as $tag) {
foreach($rss as $r){
$title = $r->title;
$content = $r->content;
$link = $tag->getAttribute('href');
echo "<a href='$link'>$title</a> <br> $content";
}
}
?>
Where i my mistake? Why it's not working and how i make it work properly?
Thanks in advance!
Both loops were going through different resources so you are just simply cross joining all records in them.
This should work to get the data you need:
<?php
$feed = file_get_contents('http://grabo.bg/rss/?city=&affid=16090');
$rss = simplexml_load_string($feed);
foreach ($rss as $key => $entry) {
if ($key == "entry")
{
$title = (string) $entry->title;
$content = (string) $entry->content;
$link = (string) $entry->link["href"];
echo "<a href='$link'>$title</a><br />" . $content;
}
}

Getting random post from rss feed

I have an rss feed, created by Yahoo Pipes and I need to get random post from it. How is it possible to realize this on php?
Read the feed using XML Parser and put it in an array. then, use array_rand to pick a random item from the array.
<?
function load_xml_feed($feed)
{
global $RanVal;
$i= 1;
$FeedXml = simplexml_load_file($feed);
foreach ($FeedXml->channel->item as $topic) {
$title[$i] = (string)$topic->title;
$link[$i] = (string)$topic->link;
$description[$i] = (string)$topic->description;
$i++;
}
$randtopic = rand(2, $i);
$link = trim($link[$randtopic]);
$title = trim($title[$randtopic]);
$description = trim($description[$randtopic]);
$RanVal = array($title,$link,$description);
return $RanVal;
}
$rss = "http://www.sabaharabi.com/rss/rss.xml";
load_xml_feed($rss);
$link = $RanVal[1];
$title = $RanVal[0];
$description = $RanVal[2];
echo "<h1>".$title."</h1><h2>".$link."</h2><p>".$description."</p>";

Categories