Good morning !
I working with the google php library and I am able to authenticate my google account to get all my events with this code:
<?php
session_start();
/////////// AUTHENTIFICATION
require_once __DIR__.'/google-api-php-client-2.2.3/vendor/autoload.php';
$redirect_uri = 'https://' . $_SERVER['HTTP_HOST'] . '/termine/index.php';
$client = new Google_Client();
$client->setAuthConfigFile('client.secrets.json');
$client->addScope("https://www.googleapis.com/auth/calendar.readonly");
$client->setAccessType("offline");
$client->setRedirectUri($redirect_uri);
$token = loadToken();
if (is_array($token) and isset($token["access_token"])) {
$client->setAccessToken($token);
if(isset($_GET['revoke'])) {
$client->revokeToken();
saveToken("");
echo('<script>location.reload();</script>');
}
// token expired
if($client->isAccessTokenExpired()) {
$refreshToken=loadRefreshToken();
try {
$newToken=$client->fetchAccessTokenWithRefreshToken($refreshToken);
} catch (\Google_Service_Exception $e) {
handle_exception($e);
}
saveToken($newToken);
}
} else { // no token --> authentification
if (!isset($_GET['code'])) {
$client->setRedirectUri($redirect_uri);
$auth_url = $client->createAuthUrl();
echo('<script>window.location.replace("'.filter_var($auth_url, FILTER_SANITIZE_URL).'");</script>');
} else {
try {
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
saveToken($token);
$client->setRedirectUri($redirect_uri);
echo('<script>window.location.replace("'.filter_var($redirect_uri, FILTER_SANITIZE_URL).'");</script>');
} catch (\Google_Service_Exception $e) {
handle_exception($e);
}
}
}
// handle google service exception
function handle_exception($e) {
global $client;
$client->revokeToken();
saveToken("");
echo('<script>location.reload();</script>');
}
function loadToken() {
return unserialize(file_get_contents("token.txt"));
}
function loadRefreshToken() {
return unserialize(file_get_contents("token_refresh.txt"));
}
function saveToken($token) {
file_put_contents("token.txt",serialize($token));
if (isset($token['refresh_token'])) file_put_contents("token_refresh.txt", serialize($token));
}
$service = new Google_Service_Calendar($client);
/////////// AUTHENTIFICATION END
/////////// CHOOSE CALENDAR
if (isset($_GET['newcal'])) unset($_SESSION['calid']);
if (isset($_GET['calid'])) { $_SESSION['calid']=$_GET['calid']; $calendarId=$_GET['calid']; }
if (!isset($_SESSION['calid'])) {
echo('<h3>Choose a calendar:</h3>');
$optParams = array( 'maxResults' => 30, 'showDeleted' => false );
$calendarList = $service->calendarList->listCalendarList($optParams);
foreach ($calendarList->getItems() as $calendarListEntry) {
if ($calendarListEntry->getAccessRole() == "owner") {
echo ("<br><a href='?calid=".$calendarListEntry->getID()."'>".$calendarListEntry->getSummary()."</a>");
}
}
} else {
echo ("<br><a href='?newcal'>choose different calendar</a>");
$calendarId = $_SESSION['calid'];
}
/////////// CHOOSE CALENDAR END
/////////// SHOW EVENTS
if (isset($calendarId)) {
$today = new DateTime("today");
$optParams = array( 'maxResults' => 300, 'timeMin' => $today->format("c"));
try {
$results = $service->events->listEvents($calendarId, $optParams);
} catch (\Google_Service_Exception $e) {
handle_exception($e);
}
if (count($results->getItems()) > 0) {
echo('<table cellpadding="5" cellspacing="5" border="1"><tr><th>Date</th><th>Time</th><th>Duration(minutes)</th><th>Description</th></tr>');
foreach ($results->getItems() as $event) {
$start= new DateTime($event->start->dateTime);
$end= new DateTime($event->end->dateTime);
$duration= date_diff($end, $start);
$minutes=$duration->h*60+$duration->i;
$summary = $event->getSummary();
echo('<tr><td>'.$start->format("j.m.Y").'</td><td>'.$start->format("G:i").'</td><td>'.$minutes.'</td><td>'.$event->getSummary().'</td></tr>');
}
echo("</table>");
}
}
/////////// SHOW EVENTS END
?>
This code shows me all events. But I would like to see, which time is free - without events (for event booking).
Is this best practices to realize this situation or can I do it better? If yes, how? :) I hope that you guys can help me :)
Thanks a lot !
Information: This is the php client library which I used: https://github.com/googleapis/google-api-php-client