如何从列表中删除具有相同名称的现有日历,并使用php在google calendar api v3中创建和插入事件作为新日历

时间:2015-02-28 04:54:32

标签: php google-calendar-api

我正在尝试在我的网站中使用php集成谷歌日历APIv3。这个想法是每个用户根据他在不同日期的订阅包中有一组事件。如果用户稍后选择一个新包,那么以前的所有事件都是订阅的包将用新的包覆盖 对我来说一切正常,除了以下内容 1)当用户到达gmail日历渲染页面时,第一次在我的日历列表的左侧菜单上成功创建了日历(例如:我的第一个作品名称),并且插入了所有事件(例如:march1上的事件, march5,march8 with different titles)
2)当用户点击链接从站点第二次渲染相同的日历时,它将再次在左侧菜单上创建我的日历列表(例如:我的第一个工作和我的第一个工作两个具有相同名称的日历)。插入所有事件之前的日历插入将在那里,第二次插入相同的日期也将在那里。(例如:在同一日期3月1日,3月5日,3月8日重复事件)
我想要的是每次当用户点击从网站点击日历页面时,应该将日历创建为新的日历(这是有效的),并且还应删除左侧菜单“我的日历列表”中具有相同名称的日历。这样在同一天可能没有重复的数据/事件。

任何人都请帮我找一个解决方案? 以下是我的代码片段

$service    = new Google_Service_Calendar($client);
$calendar   = new Google_Service_Calendar_Calendar();
$calendar->setSummary('My first work');
$calendar->setTimeZone( 'Asia/Kolkata');
$createdCalendar        = $service->calendars->insert($calendar);
$calendar               = $service->calendars->get($createdCalendar->getId());
$calendarList           = $service->calendarList->listCalendarList();
foreach($events_data_arr as $key=>$events_data_row)
{
$event = new Google_Service_Calendar_Event();
$event->setSummary($events_data_row['title']);
$event->setLocation('India');

$event->setDescription($events_data_row['description']);

$start = new Google_Service_Calendar_EventDateTime();
$start->setTimeZone('Asia/Kolkata');
$start->setDateTime($events_data_row['start_date'].'T10:00:00.000-07:00');
$event->setStart($start);

$end = new Google_Service_Calendar_EventDateTime();
$end->setTimeZone( 'Asia/Kolkata');
$end->setDateTime($events_data_row['end_date'].'T10:00:00.000-07:01');
$event->setEnd($end);

$createdEvent   = $service->events->insert($createdCalendar->getId(), $event);

}

1 个答案:

答案 0 :(得分:0)

以下代码帮助了我。

$ calendarList = $ service-> calendarList-> listCalendarList();

foreach($ calendarList-> getItems()as $ calendarListEntry){

$delid      =   $calendarListEntry->getId();
$calname    =   $calendarListEntry->getSummary();
if(strtolower(trim($calname)) == "mycalendername")
{
    $service->calendarList->delete($delid);
    $service->calendars->delete($delid);
    //echo "ID= ".$delid    ."<br/>";
}

}

我们的想法是使用

从日历列表中取出所有日历

$ calendarList = $ service-&gt; calendarList-&gt; listCalendarList();

Afetr采用那些日历名称与我们的日历名称相同的日历ID并删除该日历。仅在此步骤开始创建并插入我们的日历&#34; mycalendername&#34;。这将防止重复创建相同的日历名称到用户的主日历列表,每次使用我们的代码将创建一个带有新数据的新日历