2011-06-07 63 views
1

圍繞此主題似乎有很多問題,但似乎沒有人回答我的問題。我有一個帶有註冊表單的簡單網站,當用戶輸入他們的電子郵件時,我想將其作爲我已經設置的Google Spreadsheet中的新行進行推送。我不希望用戶認證,甚至不知道這個電子表格。如何進行身份驗證,以便我可以開始使用Google API?代碼/僞代碼將不勝感激!下面是一些例子,不回答我的問題:認證並使用Google Spreadsheet作爲網絡應用程序的數據庫

Using Google Spreadsheet as DB for apps

Google spreadsheet as db for web applications

回答

0

您將需要使用PHP和Zend G數據庫。

當您將表單發佈到您的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)); 
?> 

讓我知道這對你的作品。

相關問題