PHP Google Calendar Api HELP Required

Links for php scripts

Moderators: macek, egami, gesf

joshthomson
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

    <?php
    error_reporting(E_ALL);
    require_once 'google-api-php-client/src/Google_Client.php';
    require_once 'google-api-php-client/src/contrib/Google_CalendarService.php';
    session_start();
     
    if ((isset($_SESSION)) && (!empty($_SESSION))) {
    echo "There are cookies<br>";
    echo "<pre>";
    print_r($_SESSION);
    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->setRedirectUri('http://localhost/index.php');
    $client->setDeveloperKey('MY API KEY HERE');
    $cal = new Google_CalendarService($client);
     
    if (isset($_GET['logout'])) {
    echo "<br><br><font size=+2>Logging out</font>";
    unset($_SESSION['token']);
    }
     
    if (isset($_GET['code'])) {
    echo "<br>I got a code from Google = ".$_GET['code'];
    // You won't see this if redirected later
    $client->authenticate($_GET['code']);
    $_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";
    $client->setAccessToken($_SESSION['token']);
    }
     
    if ($client->getAccessToken()){
     
    echo "<hr><font size=+1>I have access to your calendar</font>";
    $event = new Google_Event();
    $event->setSummary('Halloween');
    $event->setLocation('The Neighbourhood');
    $start = new Google_EventDateTime();
    $start->setDateTime('2013-9-29T10:00:00.000-05:00');
    $event->setStart($start);
    $end = new Google_EventDateTime();
    $end->setDateTime('2013-9-29T10:25:00.000-05:00');
    $event->setEnd($end);
    $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>";
     
    ?>
    <html></html>


Here's a video demonstration I created:
http://www.youtube.c...h?v=m3N4YbqiYR8

Many Thanks for reading and future contributions towards helping me.
Josh

Return to “PHP Scripts”

Who is online

Users browsing this forum: No registered users and 1 guest