fetch attibute value of worldnow in rss - php

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

Related

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

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

How can I format RSS feed content description?

I would like format the my RSS feed content. Like embed some information with Description tag. I am creating Wordpress Rss feed and trying to create rss 2.0
<?xml version="1.0"?>
<rss version="2.0">
<channel>
<item>
<title>firstquestion</title>
<url>test-domain.com</url>
<description>This is some ifnormation on the description. The below are the answers for the new question</description></item>
</channel>
</rss>
Now, I want to format or further some table or information to be attached with special characters, even html tags formatting in the <description> ... How can I do that?
When I simply insert , it gives me an error?
Use CDATA sections:
$description = '<strong>Strong formatting</strong> or <em>emphasis</em>.';
$item = '<item>
<title>firstquestion</title>
<url>test-domain.com</url>
<description><![CDATA['.$description.']]></description>
</item>';
You can have HTML inside the description element, but you have to encode it using htmlspecialchars.
$description = '<strong>Strong formatting</strong> or <em>emphasis</em>.';
$item = '<item>
<title>firstquestion</title>
<url>test-domain.com</url>
<description>'.htmlspecialchars($description).'</description>
</item>';

Accessing a single XML DOM Document node

I am completely new to DOM Documents, basically what I am trying to do, is to load a RSS feed in and select only one node, and then save it to a XML file.
Here is the XML I am loading from a web feed:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Markets</title>
<description/>
<link>http://www.website.com</link>
<language>en-us</language>
<copyright>XML Output Copyright</copyright>
<ttl>15</ttl>
<pubDate>Tue, 16 Nov 2010 09:38:00 +0000</pubDate>
<webMaster>admin#website.com</webMaster>
<image>
<title>title</title>
<url>http://www.website.com/images/xmllogo.gif</url>
<link>http://www.website.com</link>
<width>144</width>
<height>16</height>
</image>
<item>
<title>title</title>
<description>the description goes here
</description>
<enclosure url="http://www.website.com/images/image.png" type="image/png"/>
</item>
</channel>
</rss>
Here is my lame attempt at getting the <description> node and saving it to feed.xml:
<?php
$feed = new DOMDocument();
$feed->load('http://www.website.com/directory/directory/cz.c');
$nodeValue = $feed->getElementsByTagName('description')->item(0)->nodeValue;
$feed->save("feed.xml");
?>
So basically I need to get the description tag, and save it as a XML file.
Any help would be appreciated, thanx in advance!
Almost correct. To get the "outerXml" of a node, you can pass the node to saveXml()
$feed = new DOMDocument();
$feed->load('http://www.website.com/directory/directory/cz.c');
$xml = $feed->saveXml($feed->getElementsByTagName('description')->item(0));
file_put_contents("feed.xml", $xml);
Saving with file_put_contents will not include an XML prolog. Note that in your example, the first description element is empty, so the file will contain <description/>.
If you want to extract the node as standalone XML Document, you have to instantiate a new DOMDocument and import the DOMNode and then use save().
$dom = new DOMDocument($feed->xmlVersion, $feed->xmlEncoding);
$dom->appendChild(
$dom->importNode(
$feed->getElementsByTagName('description')->item(0),
TRUE
)
);
echo $dom->save('new.xml');
$feed = simplexml_load_file('feed.xml');
$descr=$feed->channel->description;
Try this

Display all feeds using simplexml_load() using 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>";
}

Categories