Saturday, June 6, 2009

Reading Google Documents with PHP

At work recently I had to accomplish this and thought it might be good to spread it around. Zend provide a PHP library for reading google documents that is easy pezzy to use.

The Zend library is here
The API is here
this is some further reading at code.google.com

Here's a real basic exampl on how to read a spredsheet.......


function googleLogin($sEmail, $sPassword, $service )
{
global $objLog;
$objClient = null;
// predefined service name for Google Documents
try
{
$objClient = Zend_Gdata_ClientLogin::getHttpClient($sEmail, $sPassword, $service);
} catch (Exception $cre)
{

$sMessage = 'URL of CAPTCHA image: ' . $cre->getMessage() . "\n";
$objLog->logMessage($sMessage, 1);
}
return $objClient;
}


$service = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
$objClient = googleLogin($sEmail, $sPassword, $service);
$spreadsheetService = new Zend_Gdata_Spreadsheets($objClient);
$query = new Zend_Gdata_Spreadsheets_DocumentQuery();
$query->setSpreadsheetKey($sIdent);
$feed = $spreadsheetService->getWorksheetFeed($query);
// get the first sheet
$objWorksheet = $feed[0];
$arySpreadSheetRows = $objWorksheet->getContentsAsRows();
// get rid of the first three rows
$arySpreadSheetRows = removeHeaders($arySpreadSheetRows);
if(count($arySpreadSheetRows) == 0 )
{
$sMessage = 'no worksheet data found for '. $objWorksheet->getTitle() ;
error_log($sMessage ,1 ); exit;
}

// now we need to extract the xml data from the sheet#
$aryXmlData = getXmlDataFromFeed($arySpreadSheetRows);
// this array can then be used as you see fit

No comments:

Post a Comment