【问题标题】:Google Calendar API freeBusy multiple calendarsGoogle Calendar API freeBusy 多个日历
【发布时间】:2017-05-15 17:32:37
【问题描述】:

我已经成功地能够使用 PHP 从单个日历中获取 freeBusy 数据,但是当我尝试按照 Google 的建议在一次调用中获取多个日历时:

"items": 
  [
    {
      "id": string
    }
  ]

我似乎无法使用 PHP 来使数组对象正确,因为我尝试了 StackOverflow 上的几个示例以及我处理 PHP 数组的一般技能但没有成功。目的是将多个日历 ID 作为一个数组,然后循环遍历该数组以根据 Google API 使请求对象正确。有没有人遇到过同样的问题,并且实际上尝试过使用多个日历 ID。我有一种轻微的感觉,这可能是 Google 日历的 PHP SDK 中的一个错误。任何人都可以指出任何方向吗?这是我的单个日历 ID 的工作代码:

$client = new Google_Client();
$client->setAuthConfig("credentials/oauth-credentials.json");
$client->setScopes('https://www.googleapis.com/auth/calendar');

$client->addScope(Google_Service_Calendar::CALENDAR);
$client->useApplicationDefaultCredentials();

$cal = new Google_Service_Calendar($client);
$calendarId = 'narvik.kommune.no_2d37393134@resource.calendar.google.com';
//$calendarId = array('narvik.kommune.no_37383343730@resource.calendar.google.com','narvik.kommune.no_2d1383134@resource.calendar.google.com');
$freebusy_req = new Google_Service_Calendar_FreeBusyRequest();
$freebusy_req->setTimeMin(date(DateTime::ATOM, strtotime('2017-05-01T08:00:00.000Z')));
$freebusy_req->setTimeMax(date(DateTime::ATOM, strtotime('2017-05-19T17:00:00.000Z')));
$freebusy_req->setTimeZone($timezone);
$freebusy_req->setCalendarExpansionMax(10);
$freebusy_req->setGroupExpansionMax(10);

$item = new Google_Service_Calendar_FreeBusyRequestItem();
$item->setId($calendarId);
$freebusy_req->setItems(array($item));
$query = $cal->freebusy->query($freebusy_req);

$response_calendar = $query->getCalendars();
$busy_obj = $response_calendar[$calendarId]->getBusy();

这是结果 print_r($busy_obj);

Array
(
[0] => Google_Service_Calendar_TimePeriod Object
    (
        [end] => 2017-05-01T14:50:00Z
        [start] => 2017-05-01T09:00:00Z
        [internal_gapi_mappings:protected] => Array
            (
            )

        [modelData:protected] => Array
            (
            )

        [processed:protected] => Array
            (
            )
    )

【问题讨论】:

  • 也感谢您的帖子!我到处寻找如何使用谷歌新的 PHP 日历库。您在哪里找到如何使用Google_Service_Calendar_FreeBusyRequestGoogle_Service_Calendar_FreeBusyRequestItem

标签: php google-calendar-api


【解决方案1】:

我相信你正在寻找类似下面的东西。

$item = new Google_Service_Calendar_FreeBusyRequestItem();
$item->setId($calendarId);
$item2 = new Google_Service_Calendar_FreeBusyRequestItem();
$item2->setId($calendarId2);
$freebusy_req->setItems(array($item, $item2));

基本上你只需要创建另一个Google_Service_Calendar_FreeBusyRequestItem 并用逗号分隔两个日历项的array() 中的项目。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-23
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    • 2020-08-07
    • 1970-01-01
    相关资源
    最近更新 更多