Hey there, fellow developer! Ready to supercharge your PHP application with Google Calendar integration? You're in the right place. We'll be using the google/apiclient
package to make our lives easier. Let's dive in and get your app talking to Google Calendar in no time!
Before we jump into the code, make sure you've got:
First things first, let's get our project set up in Google Cloud Console:
Time to let Composer do its magic. Run this command in your project directory:
composer require google/apiclient
Boom! You've got the Google API client ready to roll.
Now for the fun part - let's authenticate and get that access token:
$client = new Google_Client(); $client->setAuthConfig('path/to/your/client_secret.json'); $client->addScope(Google_Service_Calendar::CALENDAR); if (isset($_GET['code'])) { $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); $client->setAccessToken($token); // Store this token for future use } elseif (!$client->getAccessToken()) { $authUrl = $client->createAuthUrl(); header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL)); exit; }
Let's get our hands dirty with some CRUD operations:
$service = new Google_Service_Calendar($client); $calendarList = $service->calendarList->listCalendarList(); foreach ($calendarList->getItems() as $calendarListEntry) { echo $calendarListEntry->getSummary(); }
$event = new Google_Service_Calendar_Event(array( 'summary' => 'Google I/O 2023', 'location' => '800 Howard St., San Francisco, CA 94103', 'description' => 'A chance to hear more about Google\'s developer products.', 'start' => array( 'dateTime' => '2023-05-28T09:00:00-07:00', 'timeZone' => 'America/Los_Angeles', ), 'end' => array( 'dateTime' => '2023-05-28T17:00:00-07:00', 'timeZone' => 'America/Los_Angeles', ), )); $calendarId = 'primary'; $event = $service->events->insert($calendarId, $event); printf('Event created: %s\n', $event->htmlLink);
$event = $service->events->get($calendarId, $eventId); $event->setSummary('Updated event title'); $updatedEvent = $service->events->update($calendarId, $event->getId(), $event);
$service->events->delete($calendarId, $eventId);
Want to level up? Here are some advanced features to explore:
$event = new Google_Service_Calendar_Event(array( 'summary' => 'Recurring Meeting', 'start' => array('dateTime' => '2023-05-01T10:00:00', 'timeZone' => 'America/New_York'), 'end' => array('dateTime' => '2023-05-01T11:00:00', 'timeZone' => 'America/New_York'), 'recurrence' => array('RRULE:FREQ=WEEKLY;BYDAY=MO,WE,FR') ));
$rule = new Google_Service_Calendar_AclRule(); $scope = new Google_Service_Calendar_AclRuleScope(); $scope->setType("user"); $scope->setValue("[email protected]"); $rule->setScope($scope); $rule->setRole("reader"); $createdRule = $service->acl->insert($calendarId, $rule);
Always be prepared for things to go wrong:
try { $event = $service->events->get($calendarId, $eventId); } catch (Exception $e) { echo 'An error occurred: ' . $e->getMessage(); }
And don't forget about rate limiting! Be a good API citizen and implement exponential backoff for retries.
There you have it! You're now equipped to integrate Google Calendar into your PHP application like a pro. Remember, the Google Calendar API is powerful and flexible, so don't be afraid to explore and experiment.
For more in-depth information, check out the official Google Calendar API documentation.
Happy coding, and may your calendars always be in sync!
Want to see it all put together? Check out our GitHub repository for a complete working example.
Now go forth and calendar-ify your PHP apps!