Reputation: 31790
I'm working on implementing a feed generator for use with Google Product Search for our sites. As Zend incorporates a feed writer class, I decided to go with Atom for the feed format.
I've done some work building up a bare-bones Atom feed into which the real product data will be injected, but I've hit a fairly serious snag.
Google want a feed file to be a customised version of either RSS or Atom, with an additional namespace attached for the tags Google Product Search uses. For example, <feed xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0">
. I've been trying to figure out how to attach the additional namespace and use it in generating the feed, but Zend's documentation on the matter is vague at best, mentioning something about extensions without going into any great detail.
I did also find mention in the documentation of registering namespaces to zend_feed, so I tried Zend_Feed::registerNamespace ('g', 'http://base.google.com/ns/1.0')
to attach the needed namespace, but this didn't appear to do anything.
So how do I add additional namespaces to a zend feed? Does it require subclassing the zend_feed_writer_feed
? is there some kind of plugin system that allows this? Or do I just need to register the namespace somehow?
Upvotes: 2
Views: 999
Reputation: 1387
Google Merchant Feed XML Atom 1.0
I have solved my Zend Framework Google Product issue. What I thought was override the main classes but I have discovered a better solution that I have used in my project.
First of all you need a Zend project :P then you need to create a new Feed Extension creating some folders in your /library/MyProject folder like these:
library/Myproject/Feed/
└── Writer
└── Extension
└── Google
├── Entry.php
├── Feed.php
└── Renderer
├── Entry.php
└── Feed.php
then you have to create your own extension. I have created my own extension Google in my own project at http://code.google.com/p/shineisp/source/browse/#svn%2Ftrunk%2Flibrary%2FShineisp%2FFeed%2FWriter%2FExtension%2FGoogle%253Fstate%253Dclosed
you are free to use my code as you like!
.
.
.
.
<entry>
<title><![CDATA[Hosting Base]]></title>
<summary><![CDATA[this is the summary.]]></summary>
<updated>2012-04-23T13:09:55+02:00</updated>
<link rel="alternate" type="text/html" href="http://www.mysite.com/hosting.html"/>
<g:id>hosting-base</g:id>
<g:availability/>
<g:google_product_category/>
<g:image_link>http://www.mysite.com/media/products/854_web-hosting-base.gif</g:image_link>
<g:price>10.89</g:price>
<g:condition>new</g:condition>
</entry>
.
.
.
.
Upvotes: 0
Reputation: 5957
Extend from Zend_Feed_Atom and add:
class Gordons_Feed_Atom extends Zend_Feed_Atom {
protected function _mapFeedHeaders($array) {
$feed = parent::_mapFeedHeaders($array);
$feed->setAttribute('xmlns:g', '"http://base.google.com/ns/1.0');
return $feed;
}
}
Update:
You will have to override the _mapFeedEntries
function and then add the entries as the others are added:
$cond = $this->_element->createElement('g:condition');
$cond->appendChild($this->_element->createCDATASection($dataentry->gcondition));
$entry->appendChild($cond);
You could always do this:
protected function _mapFeedEntries(DOMElement $root, $array)
{
parent::_mapFeedEntries($root, $array);
foreach($array as $dataentry) {
//Add you're custom ones
$cond = $this->_element->createElement('g:condition');
$cond->appendChild($this->_element->createCDATASection($dataentry->gcondition));
$entry->appendChild($cond);
}
}
That function would ensure you get the standard ones and then you're custom ones.
Upvotes: 3