2016-10-06 159 views
0

我有一些PHP代碼像這樣的日期:谷歌API PHP客戶端和服務帳戶:事件失蹤

$service = new Google_Service_Calendar($client); 

$calendarId = 'your calendar id'; 
$optParams = array(
    'timeMin' => date('c'), 
    'maxResults' => 100, 
    'singleEvents' => TRUE, 
); 

$results = $service->events->listEvents($calendarId, $optParams); 
$events = $results->getItems(); 

// in order to use it in javascript 
echo json_encode($events); 

$事件是預期的數組,但不包含每個事件的日期。在我使用服務帳戶之前,我做了一些測試,每個日期都可以通過屬性「開始」訪問,但不在我現在得到的列表中。任何想法,因爲沒有適當的文件,我應該得到什麼迴應?順便說一句。在日曆設置中更改服務帳戶的共享權限不會有幫助。

回答

0

好的,我想通了。我很困惑,因爲至少對我來說,這個文檔相當混亂。

$ events確實是正確的列表,但它不包含文檔中提到的所有屬性的原因是某些屬性必須通過方法調用來檢索。所以我們需要做的是:

// start of first event 
$startDate = $events[0]->getStart(); 

這是我的整個腳本現在看起來像。原諒我的PHP,從來沒有用過它

<?php 

    header('Content-type: application/json'); 

    include_once __DIR__ . '/vendor/autoload.php'; 

    $client = new Google_Client(); 

    $client->setAuthConfig('your service account json secret'); 

    $client->setApplicationName('your application name'); 
    $client->setScopes(['https://www.googleapis.com/auth/calendar.readonly']); 
    $service = new Google_Service_Calendar($client); 

    // make actual request 
    $calendarId = 'your calendar id'; 
    $optParams = array(
     'timeMin' => date('c'), 
     'maxResults' => 100, 
     'singleEvents' => TRUE, 
     'orderBy' => 'startTime', 
    ); 

    // of type Events.php 
    $events = $service->events; 

    // list of items of type Event.php 
    $eventItems = $events->listEvents($calendarId, $optParams)->getItems(); 

    // compose an result object, we're only interested in summary, location and dateTime atm 
    // don't know if this is considered proper php code, works though 
    $result = array(); 
    for ($i = 0; $i < count($eventItems); $i++) 
    { 
     $result[$i]->{summary} = $eventItems[$i]->getSummary(); 
     $result[$i]->{location} = $eventItems[$i]->getLocation(); 
     $result[$i]->{startDate} = $eventItems[$i]->getStart()->getDateTime(); 
    } 

    echo json_encode($result); 

?> 
相關問題