PHP-需要帮助完成GoogleSheetsAPI请求吗
我的目标是什么~ 通过接收html表单数据的.php文件写入GoogleSheets 到目前为止~ 在此处完成PHP快速入门: 已成功完成,并能够使用其示例读取/写入工作表 接下来,我使用示例代码添加了一张工作表,位于: 但它似乎缺少一段重要的代码。见下文 Linux服务器,PHP5PHP-需要帮助完成GoogleSheetsAPI请求吗,php,oauth-2.0,google-sheets-api,google-api-php-client,Php,Oauth 2.0,Google Sheets Api,Google Api Php Client,我的目标是什么~ 通过接收html表单数据的.php文件写入GoogleSheets 到目前为止~ 在此处完成PHP快速入门: 已成功完成,并能够使用其示例读取/写入工作表 接下来,我使用示例代码添加了一张工作表,位于: 但它似乎缺少一段重要的代码。见下文 Linux服务器,PHP5 <?php /* * BEFORE RUNNING: * --------------- * 1. If not already done, enable the Google Sheets API
<?php
/*
* BEFORE RUNNING:
* ---------------
* 1. If not already done, enable the Google Sheets API
* and check the quota for your project at
* https://console.developers.google.com/apis/api/sheets
* 2. Install the PHP client library with Composer. Check installation
* instructions at https://github.com/google/google-api-php-client.
*/
// Autoload Composer.
require_once __DIR__ . '/vendor/autoload.php';
$client = getClient();
$service = new Google_Service_Sheets($client);
// The ID of the spreadsheet to update.
$spreadsheetId = 'my-spreadsheet-id'; // TODO: Update placeholder value.
// The A1 notation of a range to search for a logical table of data.
// Values will be appended after the last row of the table.
$range = 'my-range'; // TODO: Update placeholder value.
// TODO: Assign values to desired properties of `requestBody`:
$requestBody = new Google_Service_Sheets_ValueRange();
$response = $service->spreadsheets_values->append($spreadsheetId, $range, $requestBody);
// TODO: Change code below to process the `response` object:
echo '<pre>', var_export($response, true), '</pre>', "\n";
function getClient() {
// TODO: Change placeholder below to generate authentication credentials. See
// https://developers.google.com/sheets/quickstart/php#step_3_set_up_the_sample
//
// Authorize using one of the following scopes:
// 'https://www.googleapis.com/auth/drive'
// 'https://www.googleapis.com/auth/drive.file'
// 'https://www.googleapis.com/auth/spreadsheets'
return null;
}
?>
我希望看到函数“getClient()”被填充。我到底需要在这里添加什么
我假设一旦填写完,我就可以保存到一个php文件并调用append,因为我的站点已经有了授权
提前谢谢
Yasiru-谢谢你的建议。我现在有以下几点~
$client->setScopes(Google_Service_Sheets::SPREADSHEETS_READONLY);
然而,当我加载页面时,我得到一个内部服务器错误500。
为了澄清,上面的PHP保存在test.PHP中,通过url调用,并位于工作目录中。从示例页面复制getClient()函数并更改以下行
'https://www.googleapis.com/auth/drive'
'https://www.googleapis.com/auth/drive.file'
到
或以下情况之一
$valueRange= new Google_Service_Sheets_ValueRange();
$valueRange->setValues(["values" => ['Value1', 'Value2']]); //The values you will bee adding
$conf = ["valueInputOption" => "RAW"];
$ins = ["insertDataOption" => "INSERT_ROWS"];
$service->spreadsheets_values->append($spreadsheetId, $range, $valueRange, $conf, $ins);
我猜你是想显示你的工作表中的数据,而不是客户。 因此,你不需要客户登录,他甚至不必知道数据来自谷歌表单(显然,除非你的网站上有这样的说明)。 最好和最安全的方法是在服务器端完成这一切 不幸的是,谷歌的文档不是很好。 这就是我的工作原理:
'https://www.googleapis.com/auth/drive'
'https://www.googleapis.com/auth/drive.file'
require_once __DIR__ . '/vendor/autoload.php'; //Path to google sheets library
$client = new \Google_Client();
$client->setApplicationName('YOURAPPNAME'); //Add a name to your project. Can be any name
$client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
$client->setAccessType('offline');
$client->setAuthConfig(__DIR__ . '/****.json');// Path to the json file with the "Service account keys"
$service = new Google_Service_Sheets($client);
$spreadsheetId = "****"; // Add your spreadsheet id. Can e found in the url of your sheet.
$range = '****'; // Name of the sheet you are working with
$valueRange= new Google_Service_Sheets_ValueRange();
$valueRange->setValues(["values" => ['Value1', 'Value2']]); //The values you will bee adding
$conf = ["valueInputOption" => "RAW"];
$ins = ["insertDataOption" => "INSERT_ROWS"];
$service->spreadsheets_values->append($spreadsheetId, $range, $valueRange, $conf, $ins);