PHP Zend导出谷歌文档与401 Unauthorized错误消息

时间:2012-05-21 15:34:08

标签: php zend-framework export-to-excel google-docs google-docs-api

我在将Google文档(spreasheet)导出到xls文件时遇到问题并保存到我们自己的服务器。

成功将文件上传到Google文档后,但在下载时以某种方式收到401错误消息。

Fatal error:  Uncaught exception 'Zend_Gdata_App_HttpException' with message 'Expected response code 200, got 401
<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>

这是我们的PHP代码,希望有人可以帮我指出出了什么问题。

任何想法都会受到高度欢迎。

<?php

// set credentials for ClientLogin authentication
$google_user = 'xxxxx'; // Your google account username
$google_pass = 'xxxxx'; // Your google account password

$service = Zend_Gdata_Docs::AUTH_SERVICE_NAME; 
$httpClient = Zend_Gdata_ClientLogin::getHttpClient($google_user, $google_pass,$service);
$docs = new Zend_Gdata_Docs($httpClient);

// Uploading file to Google Docs works perfectly as below
//$newDocumentEntry = $docs->uploadFile('test.txt', 'order-123456','text/plain', Zend_Gdata_Docs::DOCUMENTS_LIST_FEED_URI);


$path = "/var/www/vhosts/googledocs";


$docsQuery = new Zend_Gdata_Docs_Query();
$docsQuery->setTitle("My Spreadsheet Name");
$docsQuery->setTitleExact(false);
$feed = $docs->getDocumentListFeed($docsQuery);

foreach ($feed->entries as $entry) {
    $docID = $entry->getId();
    $docTitle = $entry->getTitle();  


    if($entry->getTitle() == "My spreadsheet File Name"){
        $strURL = $entry->content->getSrc() . '&exportFormat=xls&format=xls';            
        $data = $docs->get($strURL);
        file_put_contents($path."test.xls", $data);
    }
}


?>

更新 - 2012年5月23日

我们最终通过如下调整代码来实现它:对某人有用!

foreach($feed as $entry): 
        if($entry->getTitle() == 'My Spreadsheet File Name'){
            $path = "/var/www/vhosts/regustouchstone.com/subdomains/docs/httpdocs/googledocs";
            $strURL = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=".basename($entry->id)."&exportFormat=xls&format=xls";

            try{            
                $data = $service->get($strURL)->getBody();              
                $fp = fopen($path."/test.xls", "w+");
                                fwrite($fp, $data);
                                fclose($fp);                
            }catch(Zend_Exception $e) {
                echo "<br /><br /> ERROR <br />";
                echo $e->getMessage();
            }
        }
    endforeach; 

2 个答案:

答案 0 :(得分:0)

$ strUrl是否正确且一致地编码,例如都是'&amp;'被编码为'&amp;'?

此外,如果下载链接的服务器是http://spreadsheets.google.com(而不是http://docs.google.com),您可能需要对电子表格服务进行身份验证:

$spreadsheetClient = Zend_Gdata_ClientLogin::getHttpClient($google_user, $google_pass, Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME);
$spreadSheetService = new Zend_Gdata_Spreadsheets($spreadsheetClient);
...
// download the spreadsheet
$data = $spreadSheetService->get($strURL);

答案 1 :(得分:0)

我们有一个解决方案如下:

foreach($feed as $entry): 
        if($entry->getTitle() == 'My Spreadsheet File Name'){
            $path = "/var/www/vhosts/regustouchstone.com/subdomains/docs/httpdocs/googledocs";
            $strURL = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=".basename($entry->id)."&exportFormat=xls&format=xls";

            try{            
                $data = $service->get($strURL)->getBody();              
                $fp = fopen($path."/test.xls", "w+");
                                fwrite($fp, $data);
                                fclose($fp);                
            }catch(Zend_Exception $e) {
                echo "<br /><br /> ERROR <br />";
                echo $e->getMessage();
            }
        }
    endforeach;