Showing posts with label base. Show all posts
Showing posts with label base. Show all posts

Constructing unsupported XML elements using the PHP client library


When a new XML element is introduced, client library support needs to be added. At times, the services move a bit faster than the client libraries. This means that (as per the AtomPub spec) clients must store all unknown elements returned from the server. Clients may also wish to be able to use new functionality which isn't explicitly supported yet in the libraries, so clients can allow you to create arbitrary XML elements and attach them to entries.

The PHP client library accomplishes this using extension elements, represented as instances of Zend_Gdata_App_Extension_Element. Each time the server returns an element that is unknown to the client library, the client creates an instance of that class and stores it in the extensionElements array of the class representing the parent element. In order to send arbitrary XML, you can also construct a new Zend_Gdata_App_Extension_Element and place it in the extensionElements array.

As an example, Google Base added a publishing priority option, specified by adding a <gm:publishing_priority> element. At the time of this post, the element isn't supported by a class in the PHP client library, so here's how you can add it.

<?php
require_once 'Zend/Gdata/Gbase.php';
require_once 'Zend/Gdata/App/Extension/Element.php';

$base = new Zend_Gdata_Gbase();
$entry = $base->newItemEntry();

// Constructing a Zend_Gdata_App_Extension_Control
$control = $base->newControl(); 

// Constructing the extension element, and placing it into the array
// of extension owned by the class representing the parent app:control element.
// Arguments: 
//   element name (including prefix)
//   namespace prefix
//   namespace URI
//   text node of the new element
$control->extensionElements = array(
  new Zend_Gdata_App_Extension_Element('gm:publishing_priority', 'gm', 'http://base.google.com/ns-metadata/1.0', 'high')
);
$entry->control = $control;

Of course, if you discover missing elements, please feel free to file an issue in the Zend Framework issue tracker and, since it's open source, you can also contribute a fix back to the project. More information on contributing can be found on the Zend Framework wiki.

Link a Google Base item to your website


Here is how you can set the alternate link using the PHP client library. When someone clicks on your item in Google Base, he/she will be redirected to your site.
<?
$link = new Zend_Gdata_App_Extension_Link(); 
$link->setHref('http://www.example.com'); 
$link->setRel('alternate'); 
$link->setType('text/html');  
$linkArray[] = $link; 

$entry->setLink($linkArray); 
?>
Here is the underlying XML:
<link rel='alternate' type='text/html' href='http://www.example.com'/>

Setting publishing_priority attribute in .NET


For items to be published immediately using the Google Base API, you must set the publishing_priority to 'high'. The default is 'low'.

If you're using 1.2.3+ of the .NET client:
GBaseEntry entry = new GBaseEntry();
entry.PublishingPriority = new PublishingPriority("high");
GBaseEntry myEntry = service.Insert(GBaseUriFactory.Default.ItemsFeedUri, entry); 
Older versions:
GBaseEntry entry = new GBaseEntry();

XmlDocument xmlDocument = new XmlDocument();
XmlNode node = xmlDocument.CreateNode(XmlNodeType.Element, 
  "app", "control", "http://purl.org/atom/app#");
XmlNode pp = xmlDocument.CreateNode(XmlNodeType.Element, 
  "gm", "publishing_priority", "http://base.google.com/ns-metadata/1.0");

pp.InnerText = "high";
node.AppendChild(pp);

entry.ExtensionElements.Add(node);
GBaseEntry myEntry = service.Insert(GBaseUriFactory.Default.ItemsFeedUri, entry);