Displaying XML feed content - php

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;

Related

RSS images using SimpleXml not displaying.Works for .xml RSS feeds but not with .cms

I get
main(): Node no longer exists
while trying to display images (media:content)from a Rss url. I have been trying to solve it but was not successful . Can someone explain me Why this error is displaying and Help me solve it.the code works well if I Parsed RSS Feeds Urls with a .xml extension but not with a .cms Extension Urls.I Dont know why this is happening or whats wrong with my CODE.
My View
foreach ($data1 as $key1 => $value1) {
$image=$data1[$key1]['image']->children('http://search.yahoo.com/mrss/')->content->attributes()->url;
echo '<p>'.$image.'</p>';
echo '<div class="col-lg-6">';
echo '<img src="'.$image.'" /><strong style="color:pink;">'.$data1[$key1]["title"].'</strong>';
echo '<p>'.$data1[$key1]["description"].'</p>';
echo '<p>'.$data1[$key1]["pubDate"].'</p>';
echo '</div>';
}
My Controller
foreach($urls as $key => $url){
$xml = simplexml_load_file($url);
$result[$key]['title'] = $xml->channel->title;
$data = [];
for ($i=0; $i<5 ; $i++) {
# code...
$items = [];
if($xml->channel->item[$i] == null)
{
break;
}
$items['title'] = $xml->channel->item[$i]->title;
$items['link'] = $xml->channel->item[$i]->link;
$items['description'] = $xml->channel->item[$i]->description;
$items['pubDate'] = $xml->channel->item[$i]->pubDate;
$items['image'] = $xml->channel->item[$i];
$data[$i] = $items;
}
$result[$key]['data'] = $data;
//$entries = array_merge($worldFeed,$entries);
}
return View::make('index')->with('result',$result);

RSS feed limit items

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.

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

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;
}
}

Trying to read values from an XML feed and display them in PHP

I'm trying to display values from this xml feed:
http://www.scorespro.com/rss/live-soccer.xml
In my PHP code I have the following loop but it does not display the results on my page:
<?php
$xml = simplexml_load_file("http://www.scorespro.com/rss/live-soccer.xml");
echo $xml->getName() . "<br>";
foreach($xml->children() as $item)
{
echo $item->getName() . ": " . $item->name . "<br>";
}
?>
For some reason it only shows:
rss
channel:
I'm fairly new to how XML works so any help would be much appreciated.
You can get actual data from $xml->channel->item so use like below
$items = $xml->channel->item;
foreach($items as $item) {
$title = $item->title;
$link = $item->link;
$pubDate = $item->pubDate;
$description = $item->description;
}
DEMO.
you can use below code
$dom = new DOMDocument;
$dom->loadXML($url);
if (!$dom) {
echo 'Error while parsing the document';
exit;
}
$xml = simplexml_import_dom($dom);
$data = $xml->channel->item;

Categories