使用PHP和REST的Google Calendar API v3

时间:2013-07-04 18:50:14

标签: php rest zend-framework google-api google-calendar-api

我们一直在使用谷歌日历安排一些任务,我想在日历上获取数据,以便在我们的内部网站上使用。

我已使用Google API Console创建了API密钥和客户端ID,客户端密码等,并且我已启用对Calendar API的访问权限。现在我应该使用哪些PHP代码通过REST请求日历数据?

这是我到目前为止拼凑使用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>";
}

我希望能够跳过&#34;连接我!&#34;授权步骤。访问该应用的用户可能无权访问Google中的共享日历,但仍应能够在此应用中查看日历中的数据。

我的服务器上也安装了Zend Framework。我更喜欢不需要Zend的解决方案,但如果它有效,我将使用它..

1 个答案:

答案 0 :(得分:0)

我通过阅读PHP客户端库的源文档找到了我的问题的答案。具体来说是/contrib/Google_CalendarService.php