如何使用谷歌日历api将事件添加到其他人日历?

时间:2014-12-23 08:38:36

标签: php google-calendar-api

我正在尝试将活动添加到特定人的日历中, 该人是根据html格式决定的,该值存储在发送

发送邮件但未设置事件。

这是我的PHP代码..

<?php
require_once 'google-api-php-client-master/autoload.php';
require_once 'google-api-php-client-master/src/Google/Service/Calendar.php';
$client = new Google_Client();
// OAuth2 client ID and secret can be found in the Google Developers Console.
$client->setClientId('######.apps.googleusercontent.com');
$client->setClientSecret('###');
$client->setRedirectUri('https://www.example.com/oauth2callback');
$client->addScope('https://www.googleapis.com/auth/calendar');

$send = $_POST["to"];
echo "Welcome $send successfully set an event in your calendar";
if( $send == "abc")
{
    $to = "abc@gmail.com";
    $subject = "Sample Mail Project";
    $txt = $_POST["txt"];
    $headers = "From: xyz@gmail.com" . "\r\n" ;
    mail($to,$subject,$txt,$headers);
    $event = new Google_Service_Calendar_EventDateTime();
    $event->setSummary('Appointment');
    $event->setLocation('VIZAG');
    $start = new Google_Service_Calendar_Event();
    $start->setDateTime('2014-12-19T06:00:00');
    $event->setStart($start);
    $start = new Google_Service_Calendar_Event();
    $end->setDateTime('2014-12-19T10:10:00');
    $event->setEnd($end);
    $attendee1 = new EventAttendee();
    $attendee1->setEmail('abc@gmail.com');
    // ...
    $attendees = array($attendee1
        // ...
    );
    $event->attendees = $attendees;
    $createdEvent = $service->events->insert('primary', $event);
    echo $createdEvent->getId();
}

1 个答案:

答案 0 :(得分:0)

根据documentation,您应该以这种方式插入事件:

$event = new Google_Service_Calendar_Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2011-06-03T10:00:00.000-07:00');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2011-06-03T10:25:00.000-07:00');
$event->setEnd($end);
$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setEmail('attendeeEmail');
// ...
$attendees = array($attendee1,
               // ...
              );
$event->attendees = $attendees;
$createdEvent = $service->events->insert('primary', $event);

或者您可以使用quickAdd()方法执行此操作:

$createdEvent = $service->events->quickAdd(
    'primary',
    'Appointment at Somewhere on June 3rd 10am-10:25am'
);