简体   繁体   中英

Google Calendar API v3 using PHP and REST

We have been using google calendar to schedule some tasks and I would like to get data on the calendar for use on our internal website.

I have created API key's and Client ID's, Client Secret etc. using the Google API Console and I have enabled access to the Calendar API.. Now what PHP code should I use to request the calendar data over REST?

Here's what I pieced together so far using Google's PHP client library :

require_once $_SERVER['DOCUMENT_ROOT'].'/includes/google-api-php-client/src/Google_Client.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/includes/google-api-php-client/src/contrib/Google_CalendarService.php';
session_start();

    $client = new Google_Client();

$client->setApplicationName('My Application Name');

$client->setClientId('my_client_id');
$client->setClientSecret('my_client_secret');
$client->setRedirectUri('my_oauth2_callback');
$client->setDeveloperKey('my_developer_key');

$cal = new Google_CalendarService($client);

if (isset($_REQUEST['logout'])) {
  unset($_SESSION['access_token']);
}

// If the user has been redirected back to our page with an authorization code, exchange the code for an access token.
if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['access_token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['access_token'])) {
  $client->setAccessToken($_SESSION['access_token']);
}

if ($client->getAccessToken()) {
    $calList = $cal->calendarList->listCalendarList();
    print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";

            // Here's where I need to do something to get the calendar data

    // We're not done yet. Remember to update the cached access token.
    // Remember to replace $_SESSION with a real database or memcached.
    $_SESSION['token'] = $client->getAccessToken();
} else {
    $authUrl = $client->createAuthUrl();
    print "<a href='$authUrl'>Connect Me!</a>";
}

I want to be able to skip the "Connect Me!" authorization step. The user accessing the app might not have access to the shared calendar in Google but should still be able to view the data from the calendar in this app.

I also have the Zend Framework installed on my server. I prefer a solution that doesn't need Zend but if it works I'll use it..

I have found the answers to my questions by reading the source documentation for the PHP client library. Specifically /contrib/Google_CalendarService.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM