All Projects → magnetikonline → php-google-spreadsheet-api

magnetikonline / php-google-spreadsheet-api

Licence: MIT license
PHP library for read/write access to Google spreadsheets via the version 3 API.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to php-google-spreadsheet-api

OAuthLogin
第三方平台联合登陆(facebook、微信、微博、QQ、Kakao)
Stars: ✭ 57 (+50%)
Mutual labels:  oauth2
ueberauth discord
Discord OAuth2 Strategy for Überauth.
Stars: ✭ 12 (-68.42%)
Mutual labels:  oauth2
disauth
📖 A Discord OAuth2 PHP class.
Stars: ✭ 12 (-68.42%)
Mutual labels:  oauth2
OAuthLogin.AspNetCore
第三方平台联合登陆(facebook、微信、微博、QQ、Kakao)
Stars: ✭ 43 (+13.16%)
Mutual labels:  oauth2
rabbitmq-auth-backend-oauth2-spike
See rabbitmq/rabbitmq-auth-backend-oauth2 instead.
Stars: ✭ 17 (-55.26%)
Mutual labels:  oauth2
oauth2-server
A spec compliant, secure by default PHP OAuth 2.0 Server
Stars: ✭ 6,128 (+16026.32%)
Mutual labels:  oauth2
OAuth2-Go
OAuth2 sample app using Go
Stars: ✭ 19 (-50%)
Mutual labels:  oauth2
okta-spring-security-5-example
Authentication with Spring Security 5 and Okta OIDC
Stars: ✭ 16 (-57.89%)
Mutual labels:  oauth2
Twitch
[READ ONLY] Subtree split of the SocialiteProviders/Twitch Provider (see SocialiteProviders/Providers)
Stars: ✭ 20 (-47.37%)
Mutual labels:  oauth2
schematics
Schematics for adding Okta Auth to your projects
Stars: ✭ 60 (+57.89%)
Mutual labels:  oauth2
mantle
📒 Easy and effective communication for any team or community.
Stars: ✭ 30 (-21.05%)
Mutual labels:  oauth2
omniauth-okta
OAuth2 strategy for Okta
Stars: ✭ 29 (-23.68%)
Mutual labels:  oauth2
IdentityServer4.PhoneNumberAuth
Sample passwordless phone number authentication using OAuth in ASP.NET Core 2.2
Stars: ✭ 83 (+118.42%)
Mutual labels:  oauth2
portal
A hub-and-spoke platform for organizations to effectively manage their operations and data. Uses GSuite.
Stars: ✭ 26 (-31.58%)
Mutual labels:  oauth2
yii-auth-client
Yii Framework external authentication via OAuth and OpenID Extension
Stars: ✭ 20 (-47.37%)
Mutual labels:  oauth2
GoogleSignIn-iOS
Enables iOS and macOS apps to sign in with Google.
Stars: ✭ 198 (+421.05%)
Mutual labels:  oauth2
ertis-auth
Generic token generator and validator service like auth
Stars: ✭ 28 (-26.32%)
Mutual labels:  oauth2
VKontakte
[READ ONLY] Subtree split of the SocialiteProviders/VKontakte Provider (see SocialiteProviders/Providers)
Stars: ✭ 82 (+115.79%)
Mutual labels:  oauth2
native-java-examples
Native Java Apps with Micronaut, Quarkus, and Spring Boot
Stars: ✭ 44 (+15.79%)
Mutual labels:  oauth2
lumen-oauth2
OAuth2 module for the Lumen PHP framework.
Stars: ✭ 29 (-23.68%)
Mutual labels:  oauth2

Google Spreadsheets PHP API

PHP library allowing read/write access to existing Google Spreadsheets and their data. Uses the version 3 API, which is now on a deprecation path (as of February 2017) in favor of a version 4 API.

Since this API uses OAuth2 for client authentication a very lite (and somewhat incomplete) set of classes for obtaining OAuth2 tokens is included.

Requires

Methods

API()

Constructor accepts an instance of OAuth2\GoogleAPI(), which handles OAuth2 token fetching/refreshing and generation of HTTP authorization headers used with all Google spreadsheet API calls.

The included example.php provides usage examples.

API()->getSpreadsheetList()

Returns a listing of available spreadsheets for the requesting client.

$OAuth2GoogleAPI = new OAuth2\GoogleAPI(/* URLs and client identifiers */);
$OAuth2GoogleAPI->setTokenData(/* Token data */);
$OAuth2GoogleAPI->setTokenRefreshHandler(/* Token refresh handler callback */);
$spreadsheetAPI = new GoogleSpreadsheet\API($OAuth2GoogleAPI);

print_r(
	$spreadsheetAPI->getSpreadsheetList()
);

/*
[SPREADSHEET_KEY] => Array
(
	[ID] => 'https://spreadsheets.google.com/feeds/spreadsheets/private/full/...'
	[updated] => UNIX_TIMESTAMP
	[name] => 'Spreadsheet name'
)
*/

API reference

API()->getWorksheetList($spreadsheetKey)

Returns a listing of defined worksheets for a given $spreadsheetKey.

$OAuth2GoogleAPI = new OAuth2\GoogleAPI(/* URLs and client identifiers */);
$OAuth2GoogleAPI->setTokenData(/* Token data */);
$OAuth2GoogleAPI->setTokenRefreshHandler(/* Token refresh handler callback */);
$spreadsheetAPI = new GoogleSpreadsheet\API($OAuth2GoogleAPI);

print_r(
	$spreadsheetAPI->getWorksheetList('SPREADSHEET_KEY')
);

/*
[WORKSHEET_ID] => Array
(
	[ID] => 'https://spreadsheets.google.com/feeds/...'
	[updated] => UNIX_TIMESTAMP
	[name] => 'Worksheet name'
	[columnCount] => TOTAL_COLUMNS
	[rowCount] => TOTAL_ROWS
)
*/

API reference

API()->getWorksheetDataList($spreadsheetKey,$worksheetID)

Returns a read only 'list based feed' of data for a given $spreadsheetKey and $worksheetID.

List based feeds have a specific format as defined by Google - see the API reference for details. Data is returned as an array with two keys - defined headers and the data body.

$OAuth2GoogleAPI = new OAuth2\GoogleAPI(/* URLs and client identifiers */);
$OAuth2GoogleAPI->setTokenData(/* Token data */);
$OAuth2GoogleAPI->setTokenRefreshHandler(/* Token refresh handler callback */);
$spreadsheetAPI = new GoogleSpreadsheet\API($OAuth2GoogleAPI);

print_r(
	$spreadsheetAPI->getWorksheetDataList('SPREADSHEET_KEY','WORKSHEET_ID')
);

/*
Array
(
	[headerList] => Array
	(
		[0] => 'Header name #1'
		[1] => 'Header name #2'
		[x] => 'Header name #x'
	)

	[dataList] => Array
	(
		[0] => Array
		(
			['Header name #1'] => VALUE
			['Header name #2'] => VALUE
			['Header name #x'] => VALUE
		)

		[1]...
	)
)
*/

API reference

API()->getWorksheetCellList($spreadsheetKey,$worksheetID[,$cellCriteriaList])

Returns a listing of individual worksheet cells for an entire sheet, or a specific range (via $cellCriteriaList) for a given $spreadsheetKey and $worksheetID.

  • Cells returned as an array of GoogleSpreadsheet\CellItem() instances, indexed by cell reference (e.g. B1).
  • Cell instances can be modified and then passed into API()->updateWorksheetCellList() to update source spreadsheet.
  • An optional $cellCriteriaList boolean option of returnEmpty determines if method will return empty cell items.
$OAuth2GoogleAPI = new OAuth2\GoogleAPI(/* URLs and client identifiers */);
$OAuth2GoogleAPI->setTokenData(/* Token data */);
$OAuth2GoogleAPI->setTokenRefreshHandler(/* Token refresh handler callback */);
$spreadsheetAPI = new GoogleSpreadsheet\API($OAuth2GoogleAPI);

// fetch first 20 rows from third column (C) to the end of the sheet
// if $cellCriteria not passed then *all* cells for the spreadsheet will be returned
$cellCriteria = [
	'returnEmpty' = true,
	'columnStart' => 3
	'rowStart' => 1
	'rowEnd' => 20
];

print_r(
	$spreadsheetAPI->getWorksheetCellList(
		'SPREADSHEET_KEY','WORKSHEET_ID',
		$cellCriteria
	)
);

/*
Array
(
	[CELL_REFERENCE] => GoogleSpreadsheet\CellItem Object
	(
		getRow()
		getColumn()
		getReference()
		getValue()
		setValue()
		isDirty()
	)

	[CELL_REFERENCE]...
)
*/

API reference

API()->updateWorksheetCellList($spreadsheetKey,$worksheetID,$worksheetCellList)

Accepts an array of GoogleSpreadsheet\CellItem() instances as $worksheetCellList for a given $spreadsheetKey and $worksheetID, updating target spreadsheet where cell values have been modified from source via the GoogleSpreadsheet\CellItem()->setValue() method.

Given cell instances that have not been modified are skipped (no work to do).

$OAuth2GoogleAPI = new OAuth2\GoogleAPI(/* URLs and client identifiers */);
$OAuth2GoogleAPI->setTokenData(/* Token data */);
$OAuth2GoogleAPI->setTokenRefreshHandler(/* Token refresh handler callback */);
$spreadsheetAPI = new GoogleSpreadsheet\API($OAuth2GoogleAPI);

$cellList = $spreadsheetAPI->getWorksheetCellList('SPREADSHEET_KEY','WORKSHEET_ID');
$cellList['CELL_REFERENCE']->setValue('My updated value');

$spreadsheetAPI->updateWorksheetCellList(
	'SPREADSHEET_KEY','WORKSHEET_ID',
	$cellList
);

API reference

API()->addWorksheetDataRow($spreadsheetKey,$worksheetID,$rowDataList)

Add a new data row to an existing worksheet, directly after the last row. The last row is considered the final containing any non-empty cells.

Accepts a single row for insert at the bottom as an array via $rowDataList, where each array key matches a row header.

$OAuth2GoogleAPI = new OAuth2\GoogleAPI(/* URLs and client identifiers */);
$OAuth2GoogleAPI->setTokenData(/* Token data */);
$OAuth2GoogleAPI->setTokenRefreshHandler(/* Token refresh handler callback */);
$spreadsheetAPI = new GoogleSpreadsheet\API($OAuth2GoogleAPI);

$dataList = $spreadsheetAPI->getWorksheetDataList($spreadsheetKey,$worksheetID);
print_r($dataList);

/*
Array
(
	[headerList] => Array
		(
			[0] => firstname
			[1] => lastname
			[2] => jobtitle
			[3] => emailaddress
		)

	[dataList] => Array
		(
			... existing data ...
		)
)
*/

$spreadsheetAPI->addWorksheetDataRow($spreadsheetKey,$worksheetID,[
	'firstname' => 'Bob',
	'lastname' => 'Jones',
	'jobtitle' => 'UX developer',
	'emailaddress' => '[email protected]'
]);

API reference

Example

The provided example.php CLI script will perform the following tasks:

  • Fetch all available spreadsheets for the requesting client and display.
  • For the first spreadsheet found, fetch all worksheets and display.
  • Fetch a data listing of the first worksheet.
  • Fetch a range of cells for the first worksheet.
  • Finally, modify the content of the first cell fetched (commented out in example).

Setup

  • Create a new project at: https://console.developers.google.com/projectcreate.
  • Generate set of OAuth2 tokens via API Manager -> Credentials:
    • Click Create credentials drop down.
    • Select OAuth client ID then Web application.
    • Enter friendly name for client ID.
    • Enter an Authorized redirect URI - this does not need to be a real URI for the example.
    • Note down both generated client ID and client secret values.
  • Modify config.php entering redirect URI, clientID and clientSecret values generated above.
  • Visit the Allow Risky Access Permissions By Unreviewed Apps Google group and Join group using your Google account.
    • Note: For long term access it would be recommended to submit a "OAuth Developer Verification" request to Google.
  • Execute buildrequesturl.php and visit generated URL in a browser.
  • After accepting access terms and taken back to redirect URI, note down the ?code= query string value (minus the trailing # character).
  • Execute exchangecodefortokens.php, providing code from the previous step. This step should be called within a short time window before code expires.
  • Received OAuth2 token credentials will be saved to ./.tokendata.
    • Note: In a production application this sensitive information should be saved in a secure form to datastore/database/etc.

Finally, run example.php to view the result.

Note: If OAuth2 token details stored in ./.tokendata require a refresh (due to expiry), the function handler set by OAuth2\GoogleAPI->setTokenRefreshHandler() will be called to allow the re-save of updated token data back to persistent storage.

Known issues

The Google spreadsheet API documents suggest requests can specify the API version. Attempts to do this cause the cell based feed response to avoid providing the cell version slug in <link rel="edit"> nodes - making it impossible to issue an update of cell values. So for now, I have left out sending the API version HTTP header.

Reference

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].