Integrating Google Calendar into a CodeIgniter 4.5.5 application allows users to manage their events seamlessly. This integration will enable functionalities such as creating, viewing, and deleting events directly from the application. To achieve this, we will utilize the Google Calendar API, which requires setting up a Google Cloud project and obtaining the necessary credentials.
Step 1: Setting Up Google Cloud Project
- Go to the Google Cloud Console.
- Create a new project.
- Navigate to “APIs & Services” > “Library” and enable the Google Calendar API.
- Go to “Credentials” and create OAuth 2.0 credentials. Set the redirect URI to your application (e.g.,
http://yourdomain.com/auth/google
). - Download the JSON file containing your credentials.
Step 2: Install Google API Client Library
Run the following command in your CodeIgniter project directory to install the Google API Client Library via Composer:
composer require google/apiclient
Step 3: Create Google Calendar Controller
Create a new controller named GoogleCalendar.php
in the app/Controllers
directory:
<?php
namespace App\Controllers;
use Google\Client;
use Google\Service\Calendar;
class GoogleCalendar extends BaseController
{
private $client;
private $calendarService;
public function __construct()
{
$this->client = new Client();
$this->client->setApplicationName('CodeIgniter Google Calendar Integration');
$this->client->setScopes(Calendar::CALENDAR_READONLY);
$this->client->setAuthConfig('path/to/your/credentials.json');
$this->client->setAccessType('offline');
// Load previously authorized token from a file, if it exists.
if (file_exists('token.json')) {
$accessToken = json_decode(file_get_contents('token.json'), true);
$this->client->setAccessToken($accessToken);
}
// If there is no previous token or it's expired, get a new one.
if ($this->client->isAccessTokenExpired()) {
// Refresh the token if possible, else get a new one.
if ($this->client->getRefreshToken()) {
$this->client->fetchAccessTokenWithRefreshToken($this->client->getRefreshToken());
} else {
$authUrl = $this->client->createAuthUrl();
header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));
exit;
}
// Save the token to a file.
file_put_contents('token.json', json_encode($this->client->getAccessToken()));
}
$this->calendarService = new Calendar($this->client);
}
public function index()
{
$events = $this->calendarService->events->listEvents('primary', [
'maxResults' => 10,
'singleEvents' => true,
'orderBy' => 'startTime',
]);
foreach ($events->getItems() as $event) {
echo $event->getSummary() . '<br>';
}
}
}
Step 4: Update Routes
Add the following route in app/Config/Routes.php
:
$routes->get('google-calendar', 'GoogleCalendar::index');
Let’s look at what we did:
- Google Cloud Project Setup: The first step involves creating a project in the Google Cloud Console, enabling the Google Calendar API, and obtaining OAuth 2.0 credentials. This is crucial for authenticating requests to the Google Calendar service.
- Installing Google API Client Library: The Google API Client Library is installed via Composer, which simplifies the process of making API requests.
- Google Calendar Controller:
- The
GoogleCalendar
controller initializes the Google Client and sets the necessary configurations, including the application name, scopes, and authentication credentials. - It checks for an existing access token and refreshes it if necessary. If no valid token is found, it redirects the user to the Google authentication page.
- The
index
method retrieves the upcoming events from the user’s primary calendar and displays them.
- The
- Routing: The route configuration allows users to access the Google Calendar integration by navigating to
http://yourdomain.com/google-calendar
.
This integration provides a solid foundation for managing Google Calendar events within a CodeIgniter application, enhancing user experience and functionality.
Happy coding… 🙂
I’ve been designing web applications—on and off—since 2001, back when animated GIFs were all the rage and ‘responsive design’ meant answering your client’s emails. Over the past 14 years, I’ve kept pace with the ever-evolving trends in PHP development, successfully delivering a variety of projects that made my clients happy (and kept me caffeinated).
This website serves as my soapbox—a place to share the insights I’ve picked up along the way with anyone curious enough to dive in. Welcome aboard!
Need some custom work done? Or, just want to reach out? Email: dan@danoriordan.com