围绕这个主题似乎有很多问题,但似乎没有人回答我的问题。我有一个带有注册表单的简单网站,当用户输入他们的电子邮件时,我想将其作为我已经设置的 Google 电子表格中的新行推送。我不希望用户验证甚至知道这个电子表格。如何进行身份验证以便开始使用 Google API?代码/伪代码将不胜感激!以下是一些无法回答我的问题的示例:
问问题
1437 次
2 回答
2
于 2011-07-07T02:42:33.427 回答
0
您将需要使用 PHP 和 Zend GData 库。
当您将表单发布到 PHP 脚本时,您需要将所有变量收集到一个关联数组中,然后将其传递给 Zend_Gdata_Spreadsheets 的 insertRow 方法。
请务必注意,您的电子表格必须包含列标题才能使我的示例正常工作。例如名字/姓氏,重要的是要注意,当您在脚本中定位这些列标题时,它们需要全部小写并去除空格,因为这是电子表格所期望的方式。
下面是 PHP 脚本的一个基本示例:
<?php
$errors = array(); // use this to create an associative array to json encode and send back any errors
$rowData = array(); // this will be the associative array that gets passed to the insertRow method
$firstName = $_POST['firstName'];
if(isset($firstName)){
$rowData['firstname'] = $firstName; // note the key 'firstname' must equal the column header in your spreadsheet which you are inserting the value into. the column header name key should be all lowercase and not contain spaces regardless of if you have it in the spreadsheet
}else{
$errors['firstname'] = '1';
}
$lastName = $_POST['lastName'];
if(isset($lastName)){
$rowData['lastname'] = $lastName; // note the key 'lastname' must equal the column header in your spreadsheet which you are inserting the value into. the column header name key should be all lowercase and not contain spaces regardless of if you have it in the spreadsheet
}else{
$errors['lastname'] = '1';
}
set_include_path($_SERVER['DOCUMENT_ROOT'] . '/library/');
$spreadsheetKey = 'your-spreadsheet-key';
$worksheetId = 'your-worksheet-id'; // if you only have one worksheet this will be 'od6'
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);
$user = "your-user-name-at-gmail-dot-com";
$pass = "your-password";
$service = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$spreadsheetService = new Zend_Gdata_Spreadsheets($client);
$query = new Zend_Gdata_Spreadsheets_DocumentQuery();
$query->setSpreadsheetKey($spreadsheetKey);
$feed = $spreadsheetService->getWorksheetFeed($query);
global $spreadsheetService,$spreadsheetKey,$worksheetId,$rowData;
$insertedListEntry=$spreadsheetService->insertRow($rowData,$spreadsheetKey,$worksheetId);
$returnObject['success'] = 'true';
echo(json_encode($returnObject));
?>
让我知道这是否适合您。
于 2011-08-25T15:43:17.070 回答