Welcome our webmaster and SEO forum
Please enjoy the forum, contribute what you can, and wind up the Moderators!
Closed Thread
Results 1 to 3 of 3

Thread: Tutorial : Adding data to Google Base

  1. #1
    dman_2007 Guest

    Default Tutorial : Adding data to Google Base

    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.

  2. #2
    dman_2007 Guest

    Default

    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 by dman_2007; 04-18-2008 at 10:52 AM.

  3. #3
    dman_2007 Guest

    Default

    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.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124