Instead of creating the code from scratch to connect to Google Base service to perform basic operations and reinventing the wheel, we'll use Zend Framework's Gdata module. Zend framework comes with a demo script, which shows you how you can perform the basic operations. But the problem with the demo script is that it works with only a single demo item entry. Here's the script which you can use alongwith Zend framework, to perform basic operations easily :
Code:
<?php
$old = ini_get('include_path');
// windows users must use ';' instead of ':' in the line below
ini_set('include_path', $old.':/path/to/ZendFramework/library/');
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_AuthSub');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Gbase');
function getClientLoginHttpClient($user, $pass)
{
$service = Zend_Gdata_Gbase::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
return $client;
}
function insertItem($client, $itemInfo, $dryRun = false)
{
$service = new Zend_Gdata_Gbase($client);
$newEntry = $service->newItemEntry();
// Add title
$newEntry->title = $service->newTitle(trim($itemInfo['title']));
// Add some content
$newEntry->content = $service->newContent($itemInfo['content']);
$newEntry->content->type = $itemInfo['contentType'];
$newEntry->itemType = $itemInfo['itemType'];
$newEntry->itemType->type = $itemInfo['itemTypeType'];
// Add item-specific attributes
foreach($itemInfo['attributes'] as $attribute_name => $attribute_info)
{
$newEntry->addGbaseAttribute($attribute_name, $attribute_info['value'], $attribute_info['type']);
}
$createdEntry = $service->insertGbaseItem($newEntry, $dryRun);
$itemUrl = $createdEntry->id->text;
if(array_key_exists($itemInfo['media_attachments']))
{
$mediaFeed = $itemUrl . '/media/';
foreach($itemInfo['media_attachments'] as $attachment)
{
$mediaFileSource = $service->newMediaFileSource('test.png');
$mediaFileSource->setContentType('image/png');
try
{
$service->post($mediaFileSource, $mediaFeed);
}
catch (Zend_Gdata_App_Exception $e)
{
var_dump($e);
}
}
}
return $itemUrl;
}
function listAllMyItems($client)
{
$service = new Zend_Gdata_Gbase($client);
$feed = $service->getGbaseItemFeed();
return $feed;
}
function updateItem($client, $itemUrl, $newItemInfo, $dryRun = false)
{
$service = new Zend_Gdata_Gbase($client);
if($entry = $service->getGbaseItemEntry($itemUrl))
{
$entry->title = $service->newTitle($newItemInfo['title']);
$entry->content = $service->newContent($newItemInfo['content']);
$baseAttributeArr = $entry->getGbaseAttribute('pages');
if(is_object($baseAttributeArr[0]))
{
$baseAttributeArr[0]->text = $newItemInfo['item_type'];
}
$baseAttributes = $entry->getGbaseAttributes();
foreach($baseAttributes as $baseAttribute)
{
$aname = $baseAttribute->getName();
if($aname != 'customer_id' && $aname != 'item_type')
{
$entry->removeGbaseAttribute($baseAttribute);
}
}
foreach($newItemInfo['new_attributes'] as $attribute_name => $attribute_info)
{
$entry->addGbaseAttribute($attribute_name, $attribute_info['value'], $attribute_info['type']);
}
try
{
$entry->save($dryRun);
}
catch (Zend_Gdata_App_Exception $e)
{
echo "<div class='error'>ERROR:</div><br />\n";
var_dump($e);
return null;
}
if(array_key_exists($newItemInfo['media_attachments']))
{
$mediaFeed = $itemUrl . '/media/';
foreach($itemInfo['media_attachments'] as $attachment)
{
$mediaFileSource = $service->newMediaFileSource($attachment['file_path']);
$mediaFileSource->setContentType($attachment['file_type']);
try
{
$service->post($mediaFileSource, $mediaFeed);
}
catch (Zend_Gdata_App_Exception $e)
{
var_dump($e);
}
}
}
}
else
{
return null;
}
return true;
}
function deleteItem($client, $itemUrl, $dryRun = false)
{
$service = new Zend_Gdata_Gbase($client);
if ($entry = $service->getGbaseItemEntry($itemUrl))
{
try
{
$entry->delete($dryRun);
}
catch (Zend_Gdata_App_Exception $e)
{
echo "<div class='error'>ERROR:</div><br />\n";
var_dump($e);
return null;
}
}
else
{
return null;
}
}
?>