How can I format RSS feed content description? - php

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

Related

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

How to insert an image tag into XML file?

I am using the following php code to create an XML file:
echo '<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0">
<channel>
<title>mytitle</title>
<description>my description</description>
<link>www.mysite.com</link>';
$sql = "SELECT title, description, url, picture, formatted_date
FROM somewhere" ;
$result = mysql_query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
if(mysql_num_rows($result)>0)
{
while($result_array = mysql_fetch_assoc($result))
{
//timestamp date to xml format
$pubdate = date('D, d M Y H:i:s O', strtotime($result_array[formatted_date]));
echo '
<item>
<title>'.$result_array[title].'</title>
<description>'.$result_array[description].'</description>
<link>'.$result_array[indit_url].'</link>
<pubDate>'.$pubdate.' GMT</pubDate>
<enclosure url="'.$result_array[picture].'" length="1121990" type="image"/>
<image>
<url>'.$result_array[picture].'</url>
<title>image title</title>
<link>'.$result_array[picture].'</link>
<width>111</width>
<height>33</height>
<description>An amazing picture</description>
</image> </item>';
The file created is correctly validated using http://validator.w3.org/, but when i try to use some xml parser like: http://simplepie.org/ the perser are not able to capture the images. Am i using a correct tag to insert image into xml file? Is there any other tags or method to inser images?
Please try escaping the special characters in the image tag.
Use htmlspecialchars on $result_array[picture]
Also, can you show a sample of your rss feed here so that the answer can be more accurate.
I would suggest surrounding the data in an XML escape structure -as such:
<image>
<url><![CDATA[ imagedatagoeshere ]]></content></url>
</image>
However, I'm not familiar with simplepie, so I don't know if that understands CDATA properly.

PHP+XML changing node order

I've made a simple news script that saves articles to rss which then gets used on the Character Generator Newsticker on TV, the problem is that the CG plays the nodes starting from the top of the rss file.
now the xml looks like this:
<?xml version="1.0" ?>
<rss version="2.0">
<channel>
<title>News</title>
<link>website.com</link>
<description>News</description>
<language>ar-sa</language>
<item>
<title>Headline 1</title>
<description>Headline one the news this hour</description>
</item>
<item>
<title>Headline 2</title>
<description>Fire here flooding over there</description>
</item>
<item>
<title>Headline 3</title>
<description>Fire here flooding over there</description>
</item>
</channel>
</rss>
What i would like todo is have an option to move articles up and down the xml file, so instead of having "Headline 3" third in the list i would like to move it up to be first.
I know with C# you can do this using:
XElement node = ...get the element...
//Move up
if (node.PreviousNode != null) {
node.PreviousNode.AddBeforeSelf(node);
node.Remove();
}
//Move down
if (node.NextNode != null) {
node.NextNode.AddAfterSelf(node);
node.Remove();
Anyone have an idea how i can do this in PHP?
Thanks!
You can have a look at this answer XML reforming with DOM where they use the DOM-parser to rearrange the XML

PHP to add to an RSS file, without generating RSS via PHP

I need to add a new item element to my RSS file, via PHP, without just generating the RSS from PHP. I know this will require removing older items, to match the number that I want displayed, but I don't know how to add them to the file in the first place.
My code looks somewhat like this:
<rss version="2.0">
<channel>
<title>My Site Feed</title>
<link>http://www.mysitethathasfeed.com/feed/</link>
<description>
A nice site that features a feed.
</description>
<item>
<title>Launched!</title>
<link>http://www.mysitethathasfeed.com/feed/view.php?ID=launched</link>
<description>
We just launched the site! Come join the celebration!
</description>
</item>
</channel>
</rss>
// Load the XML/RSS from a file.
$rss = file_get_cotents('path_to_file');
$dom = new DOMDocument();
$dom->loadXML($rss);
Use http://php.net/manual/en/book.dom.php to learn how to modify the dom you loaded.
Extending the answer from Kyle (oh OOP inheritence), and references from the PHP Manual
<?php
$rss = file_get_contents('feed.rss');
$dom = new DOMDocument();
$dom->loadXML($rss);
// should have only 1 node in the list
$nodeList = $dom->getElementsByTagName('channel');
// assuming there's only 1 channel tag in the RSS file:
$nChannel = $nodeList->item(0);
// now create the new item
$newNode = $dom->createElement('item');
$newNode->appendChild($dom->createElement('title', 'a new title post'));
$newNode->appendChild($dom->createElement('link', 'http://www.mysitethathasfeed.com/feed/view.php?ID=launched'));
$newNode->appendChild($dom->createElement('description', 'This is the 2nd post of our feed.'));
// add item to channel
$nChannel->appendChild($newNode);
$rss = $dom->saveXML();
file_put_contents('feed.rss', $rss);

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

Categories