Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karabanov/820f3e472f2fc2c78ce78be230513aaa to your computer and use it in GitHub Desktop.
Save karabanov/820f3e472f2fc2c78ce78be230513aaa to your computer and use it in GitHub Desktop.
google sheets php гугл таблицы php
require_once 'vendor/autoload.php';
use Google\Spreadsheet\DefaultServiceRequest;
use Google\Spreadsheet\ServiceRequestFactory;
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . __DIR__ . '/my_secret.json');
/* SEND TO GOOGLE SHEETS */
$client = new Google_Client;
try{
$client->useApplicationDefaultCredentials();
$client->setApplicationName("Something to do with my representatives");
$client->setScopes(['https://www.googleapis.com/auth/drive','https://spreadsheets.google.com/feeds']);
if ($client->isAccessTokenExpired()) {
$client->refreshTokenWithAssertion();
}
$accessToken = $client->fetchAccessTokenWithAssertion()["access_token"];
ServiceRequestFactory::setInstance(
new DefaultServiceRequest($accessToken)
);
// Get our spreadsheet
$spreadsheet = (new Google\Spreadsheet\SpreadsheetService)
->getSpreadsheetFeed()
->getByTitle('Отзывы');
// Get the first worksheet (tab)
$worksheets = $spreadsheet->getWorksheetFeed()->getEntries();
$worksheet = $worksheets[0];
$listFeed = $worksheet->getListFeed();
$listFeed->insert([
'name' => "'". 'Igor',
'phone' => "'". '2425-245-224545',
'surname' => "'". 'Orlov',
'city' => "'". 'Berlin',
'age' => "'". '35',
'date' => date_create('now')->format('Y-m-d H:i:s')
]);
}catch(Exception $e){
echo $e->getMessage() . ' ' . $e->getLine() . ' ' . $e->getFile() . ' ' . $e->getCode;
}
1) Зарегистрировать приложение
2) Подключить Drive и sheets API
3) Добавить учетные данные
4) Выбрать ключ сервисного аккаунта
5) Роль: приложение -> владелец
6) Выбрать JSON
7) Руками создать таблицу на диске
8) Из полученного JSON файла взять client_email (сам емайл)
9) Вставить пример кода (изменить имя файла на свой (в примере "Отзывы"))
10) Запустить скрипт
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment