Forked from Padilo300/google sheets php гугл таблицы php
Created
October 31, 2019 05:51
-
-
Save karabanov/820f3e472f2fc2c78ce78be230513aaa to your computer and use it in GitHub Desktop.
google sheets php гугл таблицы php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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