John Davidson

php - RSS feed doesn't show the g: part in some items


I am using this code to render my rss :

$xml = new SimpleXMLExtended("<?xml version=\"1.0\" encoding=\"utf-8\"?>
<rss xmlns:g='' version='2.0'></rss>");
$channel = $xml->addChild("channel");
$channel->addChild("title", "Kidso");
$channel->addChild("link", "");
$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;
$desc_feild = "g:description";
$item->$desc_feild = NULL;
$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;

The class i am using for the CDATA sections:


class SimpleXMLExtended extends SimpleXMLElement {
public function addCData($cdata_text) {
$node = dom_import_simplexml($this);
$no = $node->ownerDocument;

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="" version="2.0">
<![CDATA[ 2 in 1 Vaiana wooden puzzle in 25 pieces ]]>
<![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. ]]>
<availability>in stock</availability>
<![CDATA[ Educa ]]>

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:

// Create a new XML document
$xml = new SimpleXMLElement('');

// Add channel element
$channel = $xml->addChild('channel');
$channel->addChild('title', 'Example RSS Feed');
$channel->addChild('link', '');
$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', '');
$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

3 of 5 based on 5016 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us