You are hereBlogs / willhall's blog / XMLWriter() for Google Base and Beyond...

XMLWriter() for Google Base and Beyond...


By willhall - Posted on 05 October 2009

I had a whale of a time trying to get some data posted onto Google Base, to enable the shop to sell items from Google Products. Although I am sure that popular e-commerce packages, and probably CMSs, all have uploader modules/components/plugins to do this for you, it is difficult trying to do a bulk export with xml so here is a quickie I wrote to help with getting the correct syntax for Google base using PHP's XMLWriter tool:

<?php
// THIS IS ABSOLUTELY ESSENTIAL - DO NOT FORGET TO SET THIS
@date_default_timezone_set("GMT");

$writer = new XMLWriter();
// Output directly to the user

$writer->openURI('php://output');
$writer->startDocument('1.0', 'UTF-8');

$writer->setIndent(4);

// declare it as an rss document that conforms to the Google Base specifications
$writer->startElement('rss');
$writer->writeAttribute('version', '2.0');
$writer->writeAttribute('xmlns:g', 'http://base.google.com/ns/1.0');
$writer->startElement("channel");
//---------------------------------------------------- replace these with you details
//$writer->writeElement('ttl', '0');
$writer->writeElement('title', 'MyCompany');
$writer->writeElement('description', 'MyCompany sells things');
$writer->writeElement('link', 'http://www.mycompany.com');
//----------------------------------------------------

//Open database connection
$dbhost = 'localhost';
$dbuser = 'username';
$dbpass = 'password';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'my_company_shop';
mysql_select_db($dbname) or die ('Error connection to database');

//Call Products from database
$productSql = 'select * from products';
$product = mysql_query($productSql);

//----------------------------------------------------
while($products = mysql_fetch_array($product)){
$writer->startElement('item');
$writer->writeElement('title', $productName['products_name']);
$writer->writeElement('g:model', $products['products_model']);
$writer->writeElement('g:department', $productToCategoriesSetName['categories_name']);
$writer->writeElement('description', $product['products_description']);
$writer->writeElement('g:price', $products['products_price']);
$writer->writeElement('g:image_link', 'http://shop.mycompany.com/images/'.$products['products_image']);
\\ this is dependant on how you link to your products
$writer->writeElement('link', 'http://shop.mycompany.com/product_info.php?product_id='.$products['products_id']);
$writer->writeElement('g:condition', 'new'); \\either it is new or used, normally i would guess things are new
$writer->writeElement('g:id', $products['products_id']);
// End Item
$writer->endElement();
}
//----------------------------------------------------
// End channel
$writer->endElement();
// End rss
$writer->endElement();
$writer->endDocument();
$writer->flush();
?>

Have fun, and contribute to google base so next time I want your product I can find it!

@willhallonline

  • exasperated at poor connections, may remove and reinstall everything! 11 hours 4 min ago
  • Government sticking to IE6 http://bit.ly/ceMrg9 "more cost effective in many cases to continue to use IE6" (what?!) 13 hours 31 min ago
  • still the mail server continues to frustrate 13 hours 38 min ago
  • feeling positive today, gonna destroy this damned mail server 18 hours 46 min ago
  • where are the open source business analysts who can help out with the actual running of companies, not just make a website for them 18 hours 47 min ago