使用php中的api从谷歌日历中获取数据

时间:2016-06-30 10:15:40

标签: php api calendar google-calendar-api

我正在使用 google-api-php-client库从我的日历中获取数据,显示错误

  

PHP致命错误:未捕获Google_Service_Exception:调用GET时出错   https://www.googleapis.com/calendar/v3/calendars/primary/events?key=my_key

这是我的代码:

<?php
include(__DIR__.'/google-api-php-client-master/src/Google/autoload.php'); 

$client = new Google_Client();
$client->setApplicationName("Google Calendar");
$client->setDeveloperKey('Zs4-y4qSHh8pDDtq7bnY5bdw'); 
$cal = new Google_Service_Calendar($client);
$calendarId = 'primary';

$events = $cal->events->listEvents($calendarId); 
$calTimeZone = $events->timeZone; 
date_default_timezone_set($calTimeZone);

foreach ($events->getItems() as $event) {


     $eventDateStr = $event->start->dateTime;
     if(empty($eventDateStr))
     {
         $eventDateStr = $event->start->date;
     }

     $temp_timezone = $event->start->timeZone;
     if (!empty($temp_timezone)) {
     $timezone = new DateTimeZone($temp_timezone);
 } else { $timezone = new DateTimeZone($calTimeZone);
     }

     $eventdate = new DateTime($eventDateStr,$timezone);
     $link = $event->htmlLink;
             $TZlink = $link . "&ctz=" . $calTimeZone;
     $newmonth = $eventdate->format("M");
     $newday = $eventdate->format("j");

    ?>
    <div class="event-container">
    <div class="eventDate">
    <span class="month"><?php

    echo $newmonth;

     ?></span><br />
    <span class="day"><?php

    echo $newday;

     ?></span><span class="dayTrail"></span>
</div>
<div class="eventBody">
    <a href="<?php echo $TZlink; ?>">

    <?php echo $event->summary;

    ?>
    </a>
</div>
</div>

提前致谢:)

0 个答案:

没有答案