dev-master
9999999-dev https://github.com/yidas/google-api-php-client-helperGoogle APIs Client Helper - Easy way to accessing Google APIs with PHP
MIT
The Requires
- php >=5.4
- google/apiclient ^2.0
helper php sdk google api google api client
Google APIs Client Helper - Easy way to accessing Google APIs with PHP
Google APIs Client Helper - Easy way to accessing Google APIs with PHP, (*2)
Easy way to develop and manage Google API application, (*4)
Documentation supported for Service SDK, (*5)
Simple usage for each Service, (*6)
This Helper is based on google-api-php-client and google-api-php-client-services., (*7)
$client = \yidas\google\apiHelper\Client::setClient() ->setApplicationName('Google API') ->setAuthConfig('/path/google_api_secret.json') ->setRedirectUri("http://{$_SERVER['HTTP_HOST']}" . dirname($_SERVER['PHP_SELF']) ->setAccessToken($accessToken) ->getClient(); if ($accessToken = ClientHelper::refreshAccessToken()) { // saveAccessToken($accessToken) } // People Service uses Google_Client from Client helper above $contacts = \yidas\google\apiHelper\services\People::getSimpleContacts();
This library requires the following:, (*8)
Run Composer in your project:, (*9)
composer require yidas/google-apiclient-helper
Then you could call it after Composer is loaded depended on your PHP framework:, (*10)
require __DIR__ . '/vendor/autoload.php'; use yidas\google\apiHelper\Client;
There are many way to set Google_Client
by Helper:, (*11)
The config keys refer to the methods of Google_Client
. For exmaple, authConfig
refers to Google_Client->setAuthConfig()
., (*12)
$client = \yidas\google\apiHelper\Client::setClient([ 'applicationName' => 'Google API', 'authConfig' => '/path/google_api_secret.json', 'redirectUri' => "http://{$_SERVER['HTTP_HOST']}" . dirname($_SERVER['PHP_SELF'], ]) ->getClient();
The methods refer to the same method names of Google_Client
. For exmaple, setAuthConfig()
refers to Google_Client->setAuthConfig()
., (*13)
$client = \yidas\google\apiHelper\Client::setClient() ->setApplicationName('Google API') ->setAuthConfig('/path/google_api_secret.json') ->setRedirectUri("http://{$_SERVER['HTTP_HOST']}" . dirname($_SERVER['PHP_SELF']) ->getClient(); ```` #### Encapsulating Method ```php $client = new Google_Client(); $client->setAuthConfig('/path/google_api_secret.json'); \yidas\google\apiHelper\Client::setClient($client);
After encapsulating Google_Client into Helper, the Helper would share with the same Google_Client object., (*14)
Simple way to get refreshed access token or false expired to skip, (*15)
public static array|false refreshAccessToken()
Example:, (*16)
$client = \yidas\google\apiHelper\Client::setClient() ->setApplicationName('Google API') ->setAuthConfig('/path/google_api_secret.json') ->setRedirectUri("http://{$_SERVER['HTTP_HOST']}" . dirname($_SERVER['PHP_SELF']) ->setAccessToken($accessToken) ->getClient(); // Simple way to get refreshed access token or false expired to skip if ($accessToken = ClientHelper::refreshAccessToken()) { // saveAccessToken($accessToken) }
Helper handles the setting
setAccessType('offline')
&setApprovalPrompt('force')
for refresh token., (*17)
Verify an access_token. This method will verify the current access_token by Google API, if one isn't provided., (*18)
public static array|false verifyAccessToken(string $accessToken=null)
Verify scopes of tokenInfo by access_token. This method will verify the current access_token by Google API, if one isn't provided., (*19)
public static array|false verifyScopes(array $scopes, string $accessToken=null)
Example:, (*20)
$result = \yidas\google\apiHelper\Client::verifyScopes([ 'https://www.googleapis.com/auth/userinfo.profile', ]);
There are more implementations such as addScope()
or createAuthUrl()
for OAuth register, you cloud refer following sample code:, (*21)
yidas/php-google-api-sample, (*22)
You could directly use any Service Helpers which uses Google_Client
from yidas\google\apiHelper\Client
:, (*23)
use \yidas\google\apiHelper\services\People as PeopleHelper; \yidas\google\apiHelper\Client::setClient([...]) $contacts = PeopleHelper::getSimpleContacts();
Or you could reset a Google_Client
for each Service Helper:, (*24)
use \yidas\google\apiHelper\services\People as PeopleHelper; PeopleHelper::setClient($googleClient); // PeopleHelper::method()...
Use getService()
to get back current Google Service object for advanced usage:, (*25)
$service = \yidas\google\apiHelper\services\People::getService(); // $service->people_connections->...
API Document: https://developers.google.com/people/api/rest/, (*26)
People helper has smart call refered to Google_Service_PeopleService_Person methods, which provides easy interface to setValue()
for a person., (*27)
// Simple setValue() example \yidas\google\apiHelper\services\People::newPerson ->setEmailAddresses('myintaer@gmail.com') ->setPhoneNumbers('+886') ->setBiographies("I'm a note");
It's easy to set attributes for a person by Helper, which provides three types for input data:, (*28)
Input by original Google Attribute Classes that are not so convenience., (*29)
$gPhoneNumber = new Google_Service_PeopleService_PhoneNumber; $gPhoneNumber->setValue('+886'); \yidas\google\apiHelper\services\People::setPhoneNumbers($gPhoneNumber);
Input by array type would map to the API key-value setting., (*30)
\yidas\google\apiHelper\services\People::setPhoneNumbers(['value' => '+886']);
Input by string type would enable Helper attribute handler which automatically settles value for all attributes., (*31)
\yidas\google\apiHelper\services\People::setPhoneNumbers('+886');
Get simple contact data with parser, (*32)
public static array getContacts()
Example:, (*33)
// Get formated list by Helper $contacts = \yidas\google\apiHelper\services\People::getSimpleContacts();
Result:, (*34)
Array ( [0] => Array ( [id] => people/c26081557840316580 [name] => Mr.Nick [email] => [phone] => 0912 345 678 ) ...
This is simple fields parser, you could use
listPeopleConnections()
if you need all fields., (*35)
Create a People Contact, (*36)
public static Google_Service_PeopleService_Person createContact()
Example:, (*37)
$person = \yidas\google\apiHelper\services\People::newPerson() ->setNames('Nick') ->setEmailAddresses('myintaer@gmail.com') ->setPhoneNumbers('+886') ->createContact();
Resource Name:
$person->resourceName
or$person['resourceName']
., (*38)
Update a People Contact, (*39)
public static Google_Service_PeopleService_PeopleEmpty updateContact(array $optParams=null)
Example:, (*40)
$person = \yidas\google\apiHelper\services\People::findByResource($resourceName) ->setNames('Nick') ->setEmailAddresses('myintaer@gmail.com') ->setPhoneNumbers('+886') ->updateContact();
Delete a People Contact, (*41)
public static Google_Service_PeopleService_PeopleEmpty deleteContact(string $resourceName=null, array $optParams=[])
Example:, (*42)
$person = \yidas\google\apiHelper\services\People::deleteContact($resourceName);
You could also use find pattern:, (*43)
$person = \yidas\google\apiHelper\services\People::findByResource($resourceName) ->deleteContact();
For all Google Exception including Client and Services:, (*44)
try {} catch (\Google_Exception $e) {}
Otherwise, for Google Services only:, (*45)
try {} catch (\Google_Service_Exception $e) {}
Google API PHP Client SDK, (*46)
Google Identity Platform, (*48)
Google People API, (*49)
Google APIs Client Helper - Easy way to accessing Google APIs with PHP
MIT
helper php sdk google api google api client