In this article, you'll learn how to work with the Google Calendar API in PHP. You'll do it by building a calendar app that allows users to add new calendars, add events, and sync calendars to Google Calendar. If you want to follow along, I recommend you to set up Homestead so you can easily get an environment for running the app.
Setting up a Google Console Project
The first thing that you need to do is to create a new project in the Google Developers Console.
Once the project is created, click on the enable and manage APIs link on the dashboard.
On the google APIs page, select and enable the Calendar API and Google+ API.
Once enabled, go back to the Google APIs page and click the Credentials link, then the add credentials button and select OAuth 2.0 client ID.
This will ask you to configure the consent screen. Click the configure consent screen to do so.
Select your preferred email address, add the product name, then click save.
Create a Web Application.
This will give you the client ID and client secret
Building the App
composer create-project --prefer-dist laravel/laravel kalendaryo
This will create a new folder named
kalendaryo which will serve as your project's directory.
Let's install some other dependencies:
composer require nesbot/carbon google/apiclient
You will be using the Google Client for talking with the Google+ API for the sign in process, and the Google Calendar API for working with Google Calendar.
Configuring the App
.env file at the root of the project directory and supply the lacking details, like so:
APP_ENV=local APP_DEBUG=true APP_KEY=base64:iZ9uWJVHemk5wa8disC8JZ8YRVWeGNyDiUygtmHGXp0= APP_URL=http://localhost DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=kalendaryo DB_USERNAME=root DB_PASSWORD=secret SESSION_DRIVER=file APP_TITLE=Kalendaryo APP_TIMEZONE="Asia/Manila" GOOGLE_CLIENT_ID="YOUR GOOGLE CLIENT ID" GOOGLE_CLIENT_SECRET="YOUR GOOGLE CLIENT SECRET" GOOGLE_REDIRECT_URL="http://kalendaryo.dev/login" GOOGLE_SCOPES="email,profile,https://www.googleapis.com/auth/calendar" GOOGLE_APPROVAL_PROMPT="force" GOOGLE_ACCESS_TYPE="offline"
The configuration values that you need to add are
APP_TIMEZONE, you can use any value from the PHP timezones page.
For the database details, create a new MySQL database and use the database name as value for
DB_PASSWORD are the login credentials for that database. If on Homestead Improved, you can just use the pre-created DB
homestead with the credentials
For the Google config, replace the value for the
GOOGLE_REDIRECT_URL with the credentials that you got from Google console earlier.
GOOGLE_SCOPES are the permissions that y
Truncated by Planet PHP, read more at the original (another 7316 bytes)
Get latest News, Widgets, Apps and much more straight into your INBOX for free. Just Enter your email address, Verify and join our Newsletter!