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).
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.
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 |
<?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!<br />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
Create the file
application/views/scripts/gdocs/index.phtml
with the following content.
1 2 3 4 5 6 7 8 9 10 |
<?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.
1 2 3 4 5 6 |
<?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 |