Integrate Google Calendar API with Codeigniter Calendar Library

Google Calendar allows users to create and edit events. Reminders can be enabled for events, with options available for type and time. Event locations can also be added, and other users can be invited to events. In this article, you will learn how to work with the Google Calendar API with Codeigniter Calendar Library. This is a very simple example, you can just copy paste, and change according to your requirement.
Follow some basic steps
  • Register a Google Application and enable Calendar API.
  • In your web application, request user for authorization of the Google Application.
  • Get the user’s timezone.
  • Use the primary calendar of the user.
  • Create an event.
Setting up a Google Console Project and enable Calendar API
Step-1 Create a new project in the Google Developers Console.
Click the Library tab on the left. Search for “Calendar API” and enable it.
Credentials tab on the left. In the next screen click on “OAuth consent screen”. Fill out the mandatory fields. Save it
Click on the “Credentials” tab (just beside “OAuth consent screen”). In the screen, click on “Create credentials”. Choose “OAuth Client ID”
Next screen fill out the name. The Application type should be “Web application”
Add a redirect url in the section Authorised redirect URIs
Require the Google API Client
Composer setup so first up we require the Google API client:
This gives us a PHP Library to communicate with the Google APIs plus a load of helper functions for each API and OAuth2.
For More information : Google API Client
Before started to implement the Google Calendar API with Codeigniter Calendar, look files structure:
  • integrate-google-calendar-api-with-codeigniter-calendar-library
    • application
      • config
        • calendar.php
        • constants.php
        • routes.php
      • libraries
        • Googleapi.php
      • controllers
        • GoogleCalendar.php
      • views
        • google-calendar
          • login.php
          • index.php
          • popup
            • create.php
            • renderadd.php
            • event.php
            • render.php
        • templates
          • header.php
          • footer.php
    • system
    • index.php
    • vendor
    • assets
      • css
        • style.css
      • js
        • custom.css
Step 2: Create new config file
Create new file named calendar.php inside “application/config/” folder.
Step 3: Create file (Googleapi)
Create a file named Googleapi.php inside “application/libraries” folder.
Step 4: Define constants
Update file named constants.php inside “application/config/” folder.
Step 5: Create a controller file
Create a controller file named GoogleCalendar.php inside “application/controllers” folder.
  • The GoogleCalendar controller handles the calendar process.
  • __construct() – Loads the required library, helper and model.
  • index() – load index file and render calendar data.
  • getCalendar() – render dynamic calendar process
  • login() – load form and process.
  • getEvents() – get google calendar event
  • addEvent() and actionEvent() – insert event in google caendar.
  • viewEvent() – fetch event from google caendar.
  • logout() – Logouts users from their account.
Step 6: Create a view(header)
Create a view file named header.php inside “application/views/templates” folder
This view contains the header section of the webpage. The Bootstrap library is used to provide a better UI, so, include it in the header section.
Step 7: Create a view(footer)
Create a view file named footer.php inside “application/views/templates” folder
This view contains the footer section of the webpage.
Step 8: Create a view(login)
Create a view file named login.php inside “application/views/google-calendar” folder.
Step 9: Create a view(index)
Create a view file named index.php inside “application/views/google-calendar” folder
Step 10: Create a view(create.php, renderadd.php, event.php and render.php)
Create a view files inside “application/views/google-calendar/popoup” folder.
a: create.php
b: renderadd.php
c: event.php
d: render.php
Step 5: Create a AJAX file
Create a js file named custom.js inside “assets/js” folder.