I am using this code to render my rss :
$xml = new SimpleXMLExtended("<?xml version=\"1.0\" encoding=\"utf-8\"?>
<rss xmlns:g='http://base.google.com/ns/1.0' version='2.0'></rss>");
$channel = $xml->addChild("channel");
$channel->addChild("title", "Kidso");
$channel->addChild("link", "https://kidso.ro");
$channel->addChild("description", "Kidso");
foreach ($res as $record) {
$gtin = $this->db->select('ean13')->where('id_product', $record['pid'])->get('bebs_product')->row();
$item = $xml->addChild("item");
$item->addChild("g:id", $record['pid']);
//$item->addChild("id", $record['pid']);
$title_feild = "g:title";
$item->$title_feild = NULL;
$item->$title_feild->addCData($record['name']);
$desc_feild = "g:description";
$item->$desc_feild = NULL;
$item->$desc_feild->addCData($record['description']);
$item->addChild("g:link", $record['url']);
$item->addChild("g:image_link", $record['main_image']);
$item->addChild("g:availability", $record['qty'] > 0 ? "in stock" : "out of stock");
$item->addChild("g:sale_price", $record['product_price']);
$item->addChild("g:gtin", ($gtin ? $gtin->ean13 : ""));
$brand_feild = "g:brand";
$item->$brand_feild = NULL;
$item->$brand_feild->addCData($record['brand_name']);
}
The class i am using for the CDATA sections:
<?php
class SimpleXMLExtended extends SimpleXMLElement {
public function addCData($cdata_text) {
$node = dom_import_simplexml($this);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection($cdata_text));
}
}
Now the CDATA items are shown correct with g:
in the begining but on the others, g:
is escaped from the names. The result is:
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
<title>Kidso</title>
<link>https://kidso.ro</link>
<description>Kidso</description>
</channel>
<item>
<id>9645</id>
<g:title>
<![CDATA[ 2 in 1 Vaiana wooden puzzle in 25 pieces ]]>
</g:title>
<g:description>
<![CDATA[ Type: 2D Age: 3+ years Number of items: 50 Product size: 70 x 50 cm. Package size: 25 x 6 x 17 cm Weight: 0.6 kg Material: Wood Additional Features: The set includes 2 puzzles of 25 pieces of environmentally sustainable and safe wood that has been treated to avoid splinters. The product allows the child to get pictures of the characters from the Vaiana Disney movie through arrangement and assembly. It is made of environmentally sustainable and safe plastic coated wood. By arranging the puzzle, the child will develop his analytical thought, logic and dexterity. Attention, the product contains small parts that the child can swallow! EDUCA is a leading brand in educational games and puzzles. It offers selected collections of high quality materials and perfect match puzzles. ]]>
</g:description>
<link>https://kidso.bg/darven-pazel-2-v-1-vaiana-9645.html</link>
<image_link>https://backoffice.kidso.com/selmatic/image/9645?code=18327923</image_link>
<availability>in stock</availability>
<sale_price>41.95</sale_price>
<gtin>8412668169494</gtin>
<g:brand>
<![CDATA[ Educa ]]>
</g:brand>
</item>
</rss>
How to prevent this escaping? I need all fields to be with g:
prefix.
My Answer:
An RSS feed in PHP can be created using the SimpleXMLElement class to generate the XML structure of the feed. Here is an example code snippet to create an RSS feed in PHP:
php
// Create a new XML document
$xml = new SimpleXMLElement(' ');
// Add channel element
$channel = $xml->addChild('channel');
$channel->addChild('title', 'Example RSS Feed');
$channel->addChild('link', 'http://www.example.com');
$channel->addChild('description', 'This is an example RSS feed');
// Add items to the feed
$item = $channel->addChild('item');
$item->addChild('title', 'Item 1');
$item->addChild('link', 'http://www.example.com/item1');
$item->addChild('description', 'This is the description for Item 1');
$item->addChild('pubDate', date('D, d M Y H:i:s O'));
// Output the XML
header('Content-Type: application/rss+xml');
echo $xml->asXML();
?>
This code snippet creates a basic RSS feed with one item. You can add more items by duplicating the item element and setting the title, link, description, and pubDate values accordingly. Remember to set the appropriate headers for the XML content type before outputting the XML.
Rate this post
4 of 5 based on 8643 votesComments