我正在努力实现一个Feed生成器,用于我们网站的Google购物搜索。由于Zend包含了一个feed feed类,我决定使用Atom来获取feed格式。
我已经做了一些工作来构建一个简单的Atom提要,其中将注入真实的产品数据,但我遇到了相当严重的障碍。
Google希望Feed文件是RSS或Atom的自定义版本,并为Google Product Search使用的代码添加了额外的命名空间。例如,<feed xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0">
。我一直在试图弄清楚如何附加额外的命名空间并将其用于生成Feed,但Zend关于这个问题的文档充其量是模糊的,提到了一些关于扩展的内容而没有详细介绍。
我在文件中也提到了将命名空间注册到zend_feed,所以我尝试Zend_Feed::registerNamespace ('g', 'http://base.google.com/ns/1.0')
来附加所需的命名空间,但这似乎没有做任何事情。
那么如何在zend Feed中添加额外的命名空间呢?它是否需要对zend_feed_writer_feed
进行子类化?是否有某种插件系统允许这个?或者我只需要以某种方式注册命名空间?
答案 0 :(得分:3)
从Zend_Feed_Atom扩展并添加:
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;
}
}
<强>更新强>
您必须覆盖_mapFeedEntries
功能,然后在添加其他条目时添加条目:
$cond = $this->_element->createElement('g:condition');
$cond->appendChild($this->_element->createCDATASection($dataentry->gcondition));
$entry->appendChild($cond);
您可以随时执行此操作:
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);
}
}
该功能将确保您获得标准功能,然后您就是自定义功能。
答案 1 :(得分:0)
Google Merchant Feed XML Atom 1.0
我已经解决了我的Zend Framework Google产品问题。我的想法是覆盖主要类,但我发现了一个更好的解决方案,我在我的项目中使用过。
首先,您需要一个Zend项目:P然后您需要创建一个新的Feed Extension,在/ library / MyProject文件夹中创建一些文件夹,如下所示:
library/Myproject/Feed/
└── Writer
└── Extension
└── Google
├── Entry.php
├── Feed.php
└── Renderer
├── Entry.php
└── Feed.php
然后你必须创建自己的扩展。我已经在http://code.google.com/p/shineisp/source/browse/#svn%2Ftrunk%2Flibrary%2FShineisp%2FFeed%2FWriter%2FExtension%2FGoogle%253Fstate%253Dclosed
我自己的项目中创建了自己的扩展Google您可以随意使用我的代码!
.
.
.
.
<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>
.
.
.
.