2017 © Pedro Peláez
 

library google-apiclient-helper

Google APIs Client Helper - Easy way to accessing Google APIs with PHP

image

yidas/google-apiclient-helper

Google APIs Client Helper - Easy way to accessing Google APIs with PHP

  • Friday, July 13, 2018
  • by yidas
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

, (*1)

Google API Client Helper


Google APIs Client Helper - Easy way to accessing Google APIs with PHP, (*2)

Latest Stable Version Latest Unstable Version License, (*3)

FEATURES

  • 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)


OUTLINE

- Reference

DEMONSTRATION

$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();

REQUIREMENTS

This library requires the following:, (*8)

  • PHP 5.4.0+
  • google/apiclient 2.0+

INSTALLATION

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;

GOOGLE CLIENT

Configuration

There are many way to set Google_Client by Helper:, (*11)

Config Array Method

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();

Config Chain Method

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)

AccessToken Usage

refreshAccessToken()

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)

verifyAccessToken()

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)

verifyScopes()

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',
]);

Implementation

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)


GOOGLE SERVICES

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->...

People

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");

Attributes

It's easy to set attributes for a person by Helper, which provides three types for input data:, (*28)

1. Origin Object

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);
2. Array

Input by array type would map to the API key-value setting., (*30)

\yidas\google\apiHelper\services\People::setPhoneNumbers(['value' => '+886']);
3. String

Input by string type would enable Helper attribute handler which automatically settles value for all attributes., (*31)

\yidas\google\apiHelper\services\People::setPhoneNumbers('+886');

getSimpleContacts()

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)

createContact()

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)

updateContact()

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();

deleteContact

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();

EXCEPTIONS

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) {}

REFERENCE

The Versions

13/07 2018

dev-master

9999999-dev https://github.com/yidas/google-api-php-client-helper

Google APIs Client Helper - Easy way to accessing Google APIs with PHP

  Sources   Download

MIT

The Requires

 

helper php sdk google api google api client