PHP Google Calendar Api HELP Required

Links for php scripts

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 2
Joined: Tue Feb 18, 2014 4:27 am

PHP Google Calendar Api HELP Required

Postby joshthomson » Tue Feb 18, 2014 4:35 am

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?

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)

Code: Select all

    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.

Return to “PHP Scripts”

Who is online

Users browsing this forum: No registered users and 1 guest