The Most Active and Friendliest
Affiliate Marketing Community Online!

“Adavice”/  “CPA

Tutorial : Adding data to Google Base

D

dman_2007

Guest
There are three ways using which you can your product listing to Google Base. They are :

1) One at a time

You can use web based listing adding tool provided at the Google Base itself to add one listing at a time. This way works best if you have only few product listings to add.

2) Data feed

Useful for adding a large number of products at once. You upload data feed file by using the browser itself or by connecting through ftp.

3) API

Most flexible way to add product listings. Yiou can code your current ecommerce platform to add product listing automatically to Google Base and to keep it synchonised.

I'll be concentrating on third way to add product listings. In my next post i'll show you how to do it.
 
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;
    }
  }
?>
 
Last edited:
Now lets see what each of the function does :

1) insertItem

This function is used for inserting new item entry. First parameter passed is an authenticated http client obtained by using getClientLoginHttpClient function. Second parameter is an array called itemInfo which contains item entry info. Following elements are required to be present in itemInfo array : title, content, contentType, itemType and itemTypeType. Other useful array element is attributes element. attributes array element is an array itself containing list of attributes which is to be added in the item entry. Each element in attributes array with attribute name as key represent an attribute which is to be added and is an array itself. The attribute array contains two elements attribute_value and attribute_type. You can also specify media attchments by using media_attachments key in itemInfo array. Array element with media_attachments key is an array itself,w ith each of its element representing a separate attachment. Each attachment array element is an array itself, with file_path and file_type array elements. Final parameter is an indicator which indicates whether to actually perform the the operation or not.

2) listAllMyItems

This function is used for getting list of all the item entries currently prsent in a user's item feed. Only parameter passed to this function is an authenticated http client.

3) updateItem

This function used for updating any current item entry. itemUrl parameter is the url of the item entry to be updated and newItemInfo parameter contains the updated item entry info.

4) deleteItem

This function is used for deleting a currently existing item entry. itemUrl parameter specifes the current url of the item which is to be deleted.
 
MI
Back