Display all feeds using simplexml_load() using PHP - php

I want to loop to get all feeds, but displaying only one
$url = "http://localhost/feeds/feeds.rss";
$xml = simplexml_load_file($url);
foreach($xml->item as $result){
echo $result->description."<br>";
}
RSS Feed is -
<channel>
<title>/</title>
<link>/</link>
<atom:link type="application/rss+xml" href="/" rel="self"/>
<description>/</description>
<language>/</language>
<ttl>/</ttl>
<item>
<title>/</title>
<description>/</description>
<pubDate>/</pubDate>
<guid>/</guid>
<link>/</link>
</item>
<item>
<title>/</title>
<description>/</description>
<pubDate>/</pubDate>
<guid>/</guid>
<link>/</link>
</item>
</channel>

The solution is:
foreach($xml->channel->item as $result){
echo $result->description."<br>";
}

Related

Select especific nodes in XML (Loop)

I can't access only one node of an xml
<destinos>
<destino>
<programas>
<item>
<location>Spain</location>
...more content
</item>
<item>
<location>USA</location>
...more content
</item>
</programas>
</destino>
<destino>
<programas>
<item>
<location>France</location>
...more content
</item>
<item>
<location>Brasil</location>
...more content
</item>
</programas>
</destino>
</destinos>
I need:
<item>
<location>Spain</location>
...more content
</item>
<item>
<location>USA</location>
...more content
</item>
<item>
<location>France</location>
...more content
</item>
<item>
<location>Brasil</location>
...more content
</item>
i tried:
header('Content-Type: application/xml');
foreach ($xml->destinos->destino->programas as $resultado){
echo $resultado->asXML();
}
or
$xml = simplexml_load_string($string);
$items = $xml->xpath("//destino/programas");
header('Content-Type: application/xml');
foreach ($items as $personaje) {
echo $personaje->asXML();
}
But I only get the first two "item" of "program" or four elements but error in xml in the second example.
Thanks for the help, I have looked for solutions but I have not been able to find any that will help me solve this problem.
This is a bit of a fudge as it 'manually' adds in a root element. The main loop uses XPath to find all of the <item> tags and then just outputs the content.
header('Content-Type: application/xml');
$xml = simplexml_load_string($string);
echo "<root>";
foreach ($xml->xpath("//item") as $item){
echo $item->asXML();
}
echo "</root>";

How can I retrieve specific XML tag names?

The feed above returns an XML document. I can successfully retrieve tag names like title,description and link using these codes
$xml = file_get_contents($feed_url);
$xml = trim($xml);
$xmlObject = new SimpleXmlElement($xml);
foreach ($xmlObject->channel->item as $item) {
$title = strip_tags($item->title);
$description = strip_tags($item->description);
}
How can I get <a10:updated> ?
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>title/title>
<link>link</link>
<description>news</description>
<item>
<guid isPermaLink="true">link</guid>
<link>link</link>
<title>Tiele</title>
<description>Descr</description>
<enclosure url="image" type="image/jpeg"/>
<a10:updated>2017-05-07T09:14:00+03:00</a10:updated>
</item>
</channel>
</rss>
Here we are using DOMDocument for extracting data from a tag.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$xml='<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>title</title>
<link>link</link>
<description>news</description>
<item>
<guid isPermaLink="true">link</guid>
<link>link</link>
<title>Tiele</title>
<description>Descr</description>
<enclosure url="image" type="image/jpeg"/>
<a10:updated>2017-05-07T09:14:00+03:00</a10:updated>
</item>
</channel>
</rss>';
$xmlObject = new DOMDocument();
$xmlObject->loadXML($xml);
$result=$xmlObject->getElementsByTagNameNS("http://www.w3.org/2005/Atom", "*");
print_r($result->item(0)->textContent);
Output:
2017-05-07T09:14:00+03:00
You're looking at a different XML namespace there. You can use curly brackets to access it:
$a10 = $item->{'a10:updated'}

fetch attibute value of worldnow in rss

I have received feed from url and I am regenerating the feed after making some changes in it So How to fetch value of "wn:size" attribute in php. Structure of feed are in following format:
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
<channel xmlns:wn="http://search.yahoo.com/mrss/" xmlns:dc="http://api.worldnow.com/cms" xmlns:media="http://purl.org/dc/elements/1.1/">
<item>
<media:thumbnail wn:size="custom" url="image url" />
</item>
</channel>
</rss>
If you want to get wn:size value then you need to parse your rss feed response as XML using simplexml_load_string function.
$xml = simplexml_load_string('your rss feed');
foreach($xml->channel->item as $item) {
$media = $item->children('media', 'http://search.yahoo.com/mrss/');
echo $media->thumbnail->attributes('wn',true)->size;
}

Symfony 2 test xml with Symfony\Component\DomCrawler\Crawler

I've got an url that return an xml but I have some problem to extract "link" element.
<rss xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
<channel>
<item>
<id>123</id>
<title>my title</title>
<link>
http://example.org
</link>
</item>
</channel>
</rss>
I need to test it with
Symfony\Component\DomCrawler\Crawler
These are my tests:
$crawler = $this->client->get('/my-feed');
$items = $crawler->filterXPath('//channel/item');
$this->assertGreaterThanOrEqual(1, $items->count()); // ok pass
// ...
$titles = $items->filterXPath('//title')->extract(array('_text'));
$this->assertContains("my title", $titles); // ok pass
// ...
$links = $items->filterXPath('//link')->extract(array('_text'));
$this->assertContains("example.org", $links); // KO!!! don't pass
var_dump($links); // empty string
"link" is a reserved word?
Your XML is broken:
you don't have a closing channel node </channel>
you don't have a closing rss node </rss>
Here is corrected XML :
<rss xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
<channel>
<item>
<id>123</id>
<title>my title</title>
<link>http://example.org</link>
</item>
</channel>
</rss>
Then, ->extract() returns An array of extracted values. So you shouldn't directly try to see its contain but get the first element and do your test:
$this->assertContains("my title", $titles[0]);
// ...
$this->assertContains("example.org", $links[0]);

PHP SimpleXML read comment with xPath

I'm loading a XML Feed, which works.
But it seems that I am missing something in my xpath query.
Actually I want to read the comment of the title node but it doesn't seem to work.
$xml = simplexml_load_file( $feed_url );
$comment = $xml->xpath('//channel/item[1]/title//comment()');
The feed has the following structure
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<title>My main feed</title>
<description>feed description</description>
<link>http://www.example.com</link>
<language>en_US</language>
<lastBuildDate>Wed, 26 Nov 2014 11:44:13 UTC</lastBuildDate>
<item>
<title><!-- Special Image --></title>
<link></link>
<guid>http://www.example.com/page/345</guid>
<media:category>horizontal</media:category>
<media:thumbnails>
<media:thumbnail url="www.example.com/test.jpg" type="image/jpeg" height="324" width="545" />
</media:thumbnails>
</item>
<item>
<title>Here's a normal title</title>
<description><![CDATA[Description Text]]></description>
<link></link>
<guid>http://www.example.com/page/123</guid>
<media:category>horizontal</media:category>
</item>
</channel>
</rss>
Does anyone have a clue how I could read the comment?
Alternatively, you could use DOMDocument to access those comments. Example:
$dom = new DOMDocument();
$dom->load($feed_url);
$xpath = new DOMXpath($dom);
$comment = $xpath->evaluate('string(//channel/item[1]/title/comment())');
echo $comment;

Categories