Home > Zend_Gdata > Creating Google Document using Zend_Gdata_Docs

Creating Google Document using Zend_Gdata_Docs

intro
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.

<?php
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,
            Zend_Gdata_Docs::lookupMimeType($fileExtension),
            Zend_Gdata_Docs::DOCUMENTS_LIST_FEED_URI
        );
        $this->view->message =
            'Document was created!Check it from your google docs page.';
    }
 
    public function logoutAction()
    {
        if (isset($this->_session->cal_token))
        {
            Zend_Gdata_AuthSub::AuthSubRevokeToken($this->_session->cal_token);
            unset($this->_session->cal_token);
        }
        $this->_helper->redirector('index');
    }
}
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 : ?>
<ul>
<li><a href="<?= $this->url(array('action'=>'create'))?>">create spreadsheet</a></li>
</ul>
<div>
<a href="<?= $this->url(array('action'=>'logout'))?>">Logout</a>
</div>
 
<?php endif; ?>

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

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

3.Check
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.
History
Date Content
2008/5/2 Published

Comments:0

Comment Form
Remember personal info

Trackbacks:0

Trackback URL for this entry
http://www.oplabo.com/article/40/trackback
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

Japanese
Search
Feeds

Return to page top