您应该可以从这里的示例代码开始:https ://code.google.com/p/google-api-php-client/source/browse/trunk/examples/calendar/simple.php
并参考这里:https ://developers.google.com/google-apps/calendar/v3/reference/events/quickAdd#examples
关于身份验证,您需要访问https://code.google.com/apis/console?api=calendar以创建和获取相关密钥,但不是很清楚,因此您可能需要玩一下。
我为 API 访问创建了一个“服务帐户”,它为我提供了客户端 ID 并让我下载了一个私钥,但如果不进一步尝试,我不确定它如何应用于上面链接的代码示例。
编辑
我刚刚发现了一些仍然有效的旧代码,它们使用 ZendGdata-1.10.2 库在日历中创建事件:
//for Google Calendar
set_include_path('./ZendGdata-1.10.2/library');
function createAlert($message, $date) {
$google = array();
// Define credentials for the Google Calendar account
$google['GCAL_USER'] = "info@example.com";
$google['GCAL_PASS'] = "password123";
// Include Zend GData client library
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
// Get Google Calendar service name (predefined service name for calendar)
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
// Authenticate with Google Calendar
$client = Zend_Gdata_ClientLogin::getHttpClient($google['GCAL_USER'], $google['GCAL_PASS'], $service);
date_default_timezone_set('GMT');
// Create a calendar object
$calendar = new Zend_Gdata_Calendar($client);
// Create a new event
$event = $calendar->newEventEntry();
$event->title = $calendar->newTitle($message);
// Add our message as the event content
$event->content = $calendar->newContent($message);
$when = $calendar->newWhen();
// Min time is 5 mins away
if ($date == "" || strtotime('+5 minute') >= strtotime($date)+300) {
$when->startTime = date("c", strtotime('+5 minute'));
$when->endTime = date("c", strtotime('+10 minute'));
} else {
$when->startTime = date("c", strtotime($date)+300);
$when->endTime = date("c", strtotime($date)+600);
}
$event->when = array($when);
// Setup reminders for event
$reminder = $calendar->newReminder();
$reminder->method = "all";
$when = $event->when[0];
$when->reminders = array($reminder);
// Send the new event to be added to Google Calendar
if (defined('GCAL_ID')) {
$newEvent = $calendar->insertEvent($event, 'http://www.google.com/calendar/feeds/' . $google['GCAL_ID'] . '/private/full');
} else {
$newEvent = $calendar->insertEvent($event);
}
// Check response
if (stripos($newEvent->id->text, "http://www.google.com/calendar/feeds/". str_replace('@', '%40', $google['GCAL_ID']) ."/private/full/") !== false) {
return "Sent!\n";
} else {
return $newEvent->id->text;
}
}