Zend Framework has many methods for working with Google Apps Platform in Zend_Gdata module. But has no method for uploading pdf file to new Google Docs API 3.0. So we created simple ZF class for uploading pdf to Google Docs
class Ext_Gdata_Uploader { const SESSION_URI = "https://docs.google.com/feeds/upload/create-session/default/private/full"; const VERSION = "3.0"; /** * The default chunk size in bytes. * * @var Integer */ protected $_chunkSize = 524288; /** * The Http Client * * @var Zend_Gdata_ */ protected $_client = null; /** * The current uri for upload the next chunk. * * @var string */ protected $_uploadUri = null; /** * The path to file for upload. * * @var string */ protected $_filelocation = null; /** * The file name in the list of entries. * * @var string */ protected $_title = null; /** * The mime type of file to upload. * * @var string */ protected $_mime = "application/pdf"; /** * The mime type of file to upload. * * @var string */ protected $_sessiontoken = null; /** * The handle of file to upload. * * @var filehanle */ protected $_filehandle = null; /** * The size of file to upload. * * @var integer */ protected $_fileSize = null; /** * The number of current chunk of file to upload. * * @var integer */ protected $_currentChunk = 1; /** * The count chunks of file to upload. * * @var integer */ protected $_countChunk = null; public function __construct($client, $filelocation, $title = "Untitled", $mime = null, $chunkSize = null) { $this->_client = $client; $this->_filelocation = $filelocation; if($chunkSize) $this->_chunkSize = $chunkSize; if($title) $this->_title = $title; if($mime) $this->_mime = $mime; $this->_sessiontoken = $this->_client->getClientLoginToken(); } private function initFile() { $this->_filehandle = fopen($this->_filelocation, "r"); if($this->_filehandle) { $this->_fileSize = filesize($this->_filelocation); rewind($this->_filehandle); $this->_countChunk = ceil($this->_fileSize/$this->_chunkSize); return true; } return false; } private function createSession() { if($this->_client) { $this->_client->setUri(self::SESSION_URI); $this->_client->setHeaders("GData-Version",self::VERSION); $this->_client->setHeaders("Content-Length","0"); $this->_client->setHeaders("Slug",$this->_title); $this->_client->setHeaders("X-Upload-Content-Type",$this->_mime); $this->_client->setHeaders("X-Upload-Content-Length", $this->_fileSize); $this->_client->setHeaders("Authorization", "GoogleLogin auth=".$this->_sessiontoken); $response = $this->_client->request('POST'); if(200 == $response->getStatus()) { $this->_uploadUri = $response->getHeader("Location"); return true; } } return false; } private function uploadNextChunk($data, $startByte, $endByte, $sizeChunk) { $this->_client->setUri($this->_uploadUri); $this->_client->setHeaders("Content-Length",$sizeChunk); $this->_client->setHeaders("Content-Range","bytes ".$startByte. "-".$endByte."/".$this->_fileSize); $this->_client->setRawData($data); return $this->_client->request('PUT'); } public function upload() { $this->initFile(); $this->createSession(); while($this->_currentChunk _countChunk) { if($this->_currentChunk == $this->_countChunk) { $startByte = (($this->_countChunk _chunkSize*($this->_countChunk-1))); $sizeChunk = $this->_fileSize % $this->_chunkSize; $sizeChunk = ($sizeChunk ? $sizeChunk : $this->_chunkSize); $data = fread($this->_filehandle, $sizeChunk); $response = $this->uploadNextChunk($data, $startByte, $this->_fileSize-1, $sizeChunk); if(201 == $response->getStatus()) return new Zend_Gdata_Entry($response->getbody()); return null; }else { $startByte = (($this->_currentChunk == 1) ? 0 : (($this->_currentChunk-1)*$this->_chunkSize)); $sizeChunk = $this->_chunkSize; $data = fread($this->_filehandle, $sizeChunk); $response = $this->uploadNextChunk($data, $startByte, ($startByte+$this->_chunkSize)-1, $this->_chunkSize); if(308 != $response->getStatus()) return false; } $this->_currentChunk++; } } public function __destruct() { if($this->_filehandle) fclose($this->_filehandle); } };
This class uses chunks pdf uploading.
You can work with this class the next way
$googleUsername ="your_username"; $googlePassword = "your_password"; $pathToPdf="/path/to/pdf"; $pdfDocumentName = "name.pdf"; $docService = Zend_Gdata_Docs::AUTH_SERVICE_NAME; $docClient = Zend_Gdata_ClientLogin::getHttpClient($googleUsername, $googlePassword, $docService); $uploader = new Ext_Gdata_Uploader($docClient, $pathToPdf, $pdfDocumentName); $entry = $uploader->Upload(); $link = $entry->getAlternateLink()->getHref();