zend框架-使用PHP和Google Docs API将表单数据保存到Google电子表格时出错


zend framework - Error Saving Form Data to Google Spreadsheets Using PHP and the Google Docs API

我需要将html表单/调查的结果发布到谷歌文档电子表格中。现在,我正在尝试使用api的最简单场景,它只是在我的电子表格中写一个简单的行,尽管我一直收到一个错误。(我不太擅长php,所以请耐心等待!)

代码:

<?php
set_include_path(get_include_path() . PATH_SEPARATOR . "$_SERVER[DOCUMENT_ROOT]/ZendGdata-1.12.1/library"); 
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_loader::loadClass('Zend_Gdata_Query');
Zend_Loader::loadClass('Zend_Gdata_Spreadsheets');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
$user = "######";
$pass = "######";
$service = "xapi"; 
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service, null, Zend_Gdata_ClientLogin::DEFAULT_SOURCE, null, null, Zend_Gdata_ClientLogin::CLIENTLOGIN_URI, 'GOOGLE');
$spreadsheetService = new Zend_Gdata_Spreadsheets($client);
$feed = $spreadsheetService->getSpreadsheetFeed();
foreach ($feed as $entry) {
echo 'Title: ' . $entry->title . ' - ';
echo 'Id: ' . $entry->id . '<br />';
}
$rowData = array('wood' => 'oak');
$spreadsheetKey = '######';
$worksheetId = 'od6';
try{
$insertedListEntry = $spreadsheetService->insertRow($rowData,
                                                    $spreadsheetKey,
                                                    $worksheetId);
}
catch(Zend_Gdata_App_HttpException $exception) {  
    echo "Error: " . $exception->getResponse()->getRawBody();  
} 
?>
</body>
</html>

错误:

> Fatal error: Uncaught exception 'Zend_Gdata_App_HttpException' with
> message 'Expected response code 200, got 401 <HTML> <HEAD>
> <TITLE>Token invalid</TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF"
> TEXT="#000000"> <H1>Token invalid</H1> <H2>Error 401</H2> </BODY>
> </HTML> ' in
> C:'xampp'htdocs'ZendGdata-1.12.1'library'Zend'Gdata'App.php:714 Stack
> trace: #0
> C:'xampp'htdocs'ZendGdata-1.12.1'library'Zend'Gdata.php(219):
> Zend_Gdata_App->performHttpRequest('GET', 'https://spreads...', Array,
> NULL, NULL, NULL) #1
> C:'xampp'htdocs'ZendGdata-1.12.1'library'Zend'Gdata'App.php(880):
> Zend_Gdata->performHttpRequest('GET', 'https://spreads...', Array) #2
> C:'xampp'htdocs'ZendGdata-1.12.1'library'Zend'Gdata'App.php(768):
> Zend_Gdata_App->get('https://spreads...', NULL) #3
> C:'xampp'htdocs'ZendGdata-1.12.1'library'Zend'Gdata'App.php(210):
> Zend_Gdata_App->importUrl('https://spreads...', 'Zend_Gdata_Spre...',
> NULL) #4 C:'xampp'htdocs'ZendGdata-1.12.1'library'Zend'Gdata.php(162):
> Zend_Gdata_App->getFeed('https://spreads...', 'Zend_Gdata_Spre...') #5
> C:'xa in C:'xampp'htdocs'ZendGdata-1.12.1'library'Zend'Gdata'App.php
> on line 714

感谢您的帮助!:]

我已经有一段时间没有处理这个问题了,但我相信您看到的特定错误消息与您使用的服务名称有关。'xapi'是谷歌服务的通用服务名称,但您正试图专门访问谷歌电子表格。

尝试更改:

$service = 'xapi';

收件人:

$service = 'wise';

由于'wise'是Google电子表格的正确ClientLogin服务名称。

您的错误消息引用了一个无效的令牌,这意味着Google不接受您的登录。我认为错误在$service行。试试这个:

$user = //user name @ gmail here
$pass = //password here
$sheet = //spreadsheet id/key here
$worksheet = "od6";
require_once('Zend/Loader.php');
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Spreadsheets');
Zend_Loader::loadClass('Zend_Http_Client');
$service = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$spreadsheetService = new Zend_Gdata_Spreadsheets($client);

那么你所要做的就是:

$rowData = array("column1"=>"blah", "column2"=>"blah"); //replace column1 and column2 with your actual column names and replace blah with the information you want to add
$insertedListEntry = $spreadsheetService->insertRow($rowData,
                                                    $spreadsheetKey,
                                                    $worksheetId);