Jump to content

Require Help - PHP Google Calendar Api


Recommended Posts

Good Afternoon All,


I have set out to create a script which will hopefully add an event to an existing google calendar (as a background process using details recorded during the completion of a form) the script to initalise on the submission of the form.


However, I have been unable to achieve any results whilst working on this task, I have only witnessed blank screens. Perhaps I am doing something wrong?


Can you help?




The required steps:


1. user completes form including date from & date to.

2. user clicks submit

3. onclick(); starts php script

4. php script takes the dates as variables

5. php script uses the predefined google account username and password to authenticate

6. User returns to homepage (Google event added)



This is my code so far: (removed ID's for privacy)

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_CalendarService.php';

if ((isset($_SESSION)) && (!empty($_SESSION))) {
   echo "There are cookies<br>";
   echo "<pre>";
   echo "</pre>";
// This is the file I am using ,,, i have cut out the ID's for privacy
$client = new Google_Client();
$client->setApplicationName("Google Calendar PHP Starter Application");
$client->setClientId('MY CLI ID HERE');
$client->setClientSecret('MY CLI SECRECT KEY HERE');
$client->setDeveloperKey('MY API KEY HERE');
$cal = new Google_CalendarService($client);

if (isset($_GET['logout'])) {
  echo "<br><br><font size=+2>Logging out</font>";

if (isset($_GET['code'])) {
  echo "<br>I got a code from Google = ".$_GET['code'];
// You won't see this if redirected later
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
  echo "<br>I got the token = ".$_SESSION['token'];
// <-- not needed to get here unless location uncommented

if (isset($_SESSION['token'])) {
  echo "<br>Getting access";

if ($client->getAccessToken()){

  echo "<hr><font size=+1>I have access to your calendar</font>";
  $event = new Google_Event();
  $event->setLocation('The Neighbourhood');
  $start = new Google_EventDateTime();
  $end = new Google_EventDateTime();
  $createdEvent = $cal->events->insert('###', $event);
  echo "<br><font size=+1>Event created</font>";

  echo "<hr><br><font size=+1>Already connected</font> (No need to login)";

} else {

  $authUrl = $client->createAuthUrl();
  print "<hr><br><font size=+2><a href='$authUrl'>Connect Me!</a></font>";


$url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
echo "<br><br><font size=+2><a href=$url?logout>Logout</a></font>";


Here's a video demonstration I created:





Many Thanks for reading and future contributions towards helping me.



Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.