使用PHP访问Google日历api


Google calendar api access with PHP

我对google api有点困惑,尤其是google日历api。似乎我找到的每个示例都希望用户通过返回URL连接并返回。所有我想做的是在后台与谷歌日历api抓取信息,显示数据,甚至创建数据。我愿意这样做与Javascript/JSON或通过PHP最好。

下面是我找到的例子:

<?php
session_start();
require_once dirname(__FILE__).'/GoogleClientApi/Google_Client.php';
require_once dirname(__FILE__).'/GoogleClientApi/contrib/Google_AnalyticsService.php';
$scriptUri = "http://".$_SERVER["HTTP_HOST"].$_SERVER['PHP_SELF'];
$client = new Google_Client();
$client->setAccessType('online'); // default: offline
$client->setApplicationName('My Application name');
$client->setClientId('INSERT HERE');
$client->setClientSecret('INSERT HERE');
$client->setRedirectUri($scriptUri);
$client->setDeveloperKey('INSERT HERE'); // API key
// $service implements the client interface, has to be set before auth call
$service = new Google_AnalyticsService($client);
if (isset($_GET['logout'])) { // logout: destroy token
    unset($_SESSION['token']);
    die('Logged out.');
}
if (isset($_GET['code'])) { // we received the positive auth callback, get the token and store it in session
    $client->authenticate();
    $_SESSION['token'] = $client->getAccessToken();
}
if (isset($_SESSION['token'])) { // extract token from session and configure client
    $token = $_SESSION['token'];
    $client->setAccessToken($token);
}
if (!$client->getAccessToken()) { // auth call to google
    $authUrl = $client->createAuthUrl();
    header("Location: ".$authUrl);
    die;
}
echo 'Hello, world.';

我明白了。如果有人好奇这是如何工作的,无论你正在使用的谷歌API,你首先需要允许通过浏览器访问,除非你已经有一个访问令牌。在此之后,您可以从他们的库中提取您需要的任何信息。