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

时间:2013-02-12 22:01:15

标签: php zend-framework google-docs-api google-spreadsheet-api

我需要将html表单/调查的结果发布到Google文档电子表格中。现在我正在尝试使用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

任何帮助表示赞赏! :

2 个答案:

答案 0 :(得分:1)

自从我使用它以来已经有一段时间了,但我相信您看到的具体错误消息与您正在使用的服务名称有关。 'xapi'是Google服务的通用服务名称,但您正在尝试专门访问Google Spreadsheets。

尝试更改:

$service = 'xapi';

要:

$service = 'wise';

由于'wise'是Google电子表格的正确ClientLogin service name

答案 1 :(得分:0)

您的错误消息是指无效令牌,表示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);