Home > Zend_Gdata > Creating Google Document using Zend_Gdata_Docs

Creating Google Document using Zend_Gdata_Docs

This article shows code to upload a csv file and create a google document using Zend_Gdata_Docs.
Please see Google Document List Data API for more detail information.
To execute the following code, you need to have a google account that can use google document.
You can check the screen from demonstration site(though I don't know about security).

1.Create Controller
Create the file application/constorllers/GdocsController.php with the following content.

require_once 'Zend/Controller/Action.php';
require_once 'Zend/Gdata.php';
require_once 'Zend/Gdata/AuthSub.php';
require_once 'Zend/Gdata/Docs.php';
require_once 'Zend/Session/Namespace.php';
class GdocsController extends Zend_Controller_Action
    protected $_session;
    protected $_templateFile = '/path/to/template.csv';
    protected $_title = 'Test Spreadsheet';
    public function preDispatch()
        $this->_session = new Zend_Session_Namespace('GdocsController');
    public function indexAction()
        if (!isset($this->_session->cal_token)) {
            if (isset($_GET['token'])) {
                // You can convert the single-use token to a session token.
                $token = $_GET['token'];
                $session_token =  Zend_Gdata_AuthSub::getAuthSubSessionToken($token);
                // Store the session token in our session.
                $this->_session->cal_token = $session_token;
            } else {
                // Display link to generate single-use token
                $url = 'http://'. $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
                $scope = 'http://docs.google.com/feeds/documents';
                $secure = false;
                $session = true;
                $googleUri = Zend_Gdata_AuthSub::getAuthSubTokenUri(
                    $url, $scope, $secure, $session);
                $this->view->message =
                    "Go <a href='$googleUri'>Google Login Screen</a>";
                return $this->render();
    public function createAction()
        $client = Zend_Gdata_AuthSub::getHttpClient($this->_session->cal_token);
        $docs = new Zend_Gdata_Docs($client);
        $newDocumentEntry = $docs->uploadFile(
            $this->_templateFile, $this->_title,
        $this->view->message =
            'Document was created!Check it from your google docs page.';
    public function logoutAction()
        if (isset($this->_session->cal_token))
2.Create View
Create the file application/views/scripts/gdocs/index.phtml with the following content.

<?php if (isset($this->message)) : ?>
<?= $this->message ?>
<?php else : ?>
<li><a href="<?= $this->url(array('action'=>'create'))?>">create spreadsheet</a></li>
<a href="<?= $this->url(array('action'=>'logout'))?>">Logout</a>
<?php endif; ?>

Create the file application/views/scripts/gdocs/create.phtml with the following content.

<?php if (isset($this->message)) : ?>
<?= $this->message ?>
<?php endif; ?>
<a href="<?= $this->url(array('action'=>'index'))?>">Back</a>

Access the web server and execute creation of google document after you logged in from google's login screen. Then you can check the document from your google docs page.
Date Content
2008/5/2 Published


Comment Form
Remember personal info


Trackback URL for this entry
Listed below are links to weblogs that reference
Creating Google Document using Zend_Gdata_Docs from Open Programming Laboratory

Home > Zend_Gdata > Creating Google Document using Zend_Gdata_Docs


Return to page top