PHP code example of tomshaw / google-api

1. Go to this page and download the library: Download tomshaw/google-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tomshaw / google-api example snippets


namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use TomShaw\GoogleApi\GoogleClient;
use Illuminate\Http\Request;

class GoogleAuthController extends Controller
{
    public function index(GoogleClient $client)
    {
        return redirect()->away($client->createAuthUrl());
    }

    public function callback(Request $request, GoogleClient $client)
    {
        $accessToken = $client->fetchAccessTokenWithAuthCode($request->get('code'));

        $client->setAccessToken($accessToken);

        return redirect()->route('homepage');
    }
}

use TomShaw\GoogleApi\GoogleApi;
use TomShaw\GoogleApi\Exceptions\TokenNotFoundException;

try {
    $events = GoogleApi::calendar()->listEvents();
} catch (TokenNotFoundException $e) {
    return redirect()->away($e->authUrl);
}

$token = $client->getAccessToken();

$token->accessToken;        // string|null
$token->isExpired();        // bool
$token->hasRefreshToken();  // bool
$token->toArray();          // array<string, int|string>

use TomShaw\GoogleApi\GoogleApi;

GoogleApi::forUser($user)
    ->gmail()
    ->from('[email protected]', 'Billing')
    ->to($user->email, $user->name)
    ->subject('Your invoice')
    ->message('<p>Thanks!</p>')
    ->send();

use Google\Service\Sheets;
use TomShaw\GoogleApi\GoogleApi;

$sheets = GoogleApi::service(Sheets::class);

'token_storage_adapter' => TomShaw\GoogleApi\Storage\DatabaseStorageAdapter::class,

php artisan vendor:publish --provider="TomShaw\GoogleApi\Providers\GoogleApiServiceProvider" --tag=config

php artisan migrate