0
<?php

session_start();
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_CalendarService.php';

$client = new Google_Client();
$client->setApplicationName("Google Calendar PHP Starter Application");

$client->setUseObjects(true);
$cal = new Google_CalendarService($client);

if (isset($_GET['logout'])) {
  unset($_SESSION['token']);
}

if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}

$service = new Google_CalendarService($client);
if ($client->getAccessToken()) {
    $_SESSION['token'] = $client->getAccessToken();
}
//$rightNow = date('c');
//$params = array('singleEvents' => 'true', 'orderBy' => 'startTime', 'timeMin' => $rightNow);
$rightNow = date('c');

$tomorrow_in_seconds = 60 * 60 * 24;
$tomorrow = date('yyyy-mm-dd', date("Y-m-d\Th:i:s", time() + $tomorrow_in_seconds));

$params = array('singleEvents' => 'true', 'orderBy' => 'startTime', 'timeMin' => $rightNow, 'timeMax' => $tomorrow);
$events = $service->events->listEvents('primary', $params);

foreach ($events->getItems() as $event) {
        echo $event->getLocation()."<br/>";
        echo $event->when[0]->startTime;
        echo $event->when[0]->endTime;

        echo '-----<br />';
}


?>

我得到了活动的地点。但无法检索 startTime 和 endTime。还有一种方法可以只显示今天的事件吗?最终,当开始时间和当前时间相同时,我想在我的网站上显示一个弹出窗口。那么任何人都可以帮我获得 StartTime 吗?

我看过这里: https ://developers.google.com/google-apps/calendar/v3/reference/events/list 但它没有告诉如何获取开始时间和结束时间。这真的很有必要。PHP 不是我的强项。那么任何人都可以帮助我吗?

谢谢你。


经过长时间的研究,我能够提取信息。但只能用 Zend Framework 来做。好吧,希望这对其他人有帮助。

require_once 'Zend/Loader.php';
    Zend_Loader::loadClass('Zend_Gdata');
    Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
    Zend_Loader::loadClass('Zend_Gdata_Calendar');
    Zend_Loader::loadClass('Zend_Http_Client');

    // create authenticated HTTP client for Calendar service
    $gcal = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
    $user = "your-email@gmail.com";
    $pass = "your-password";
    $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $gcal);
    $gcal = new Zend_Gdata_Calendar($client);

    // generate query to get event list
    $query = $gcal->newEventQuery();
    $query->setUser('default');
    $query->setVisibility('private');
    $query->setProjection('basic');

    // get and parse calendar feed
    // print output
    try {
      $feed = $gcal->getCalendarEventFeed($query);
    } catch (Zend_Gdata_App_Exception $e) {
      echo "Error: " . $e->getResponse();
    }
    ?>
     <h1><?php echo $feed->title; ?></h1> <!-- displays email title -->
    <ol>

    <?php       



        $today = date('yyyy-mm-dd');
        $tomorrow = date(DateTime::ATOM, time()+(1 * 24 * 60 * 60));

        $gdataCal = new Zend_Gdata_Calendar($client);
        $query = $gdataCal->newEventQuery();
        $query->setUser('default');
        $query->setVisibility('private');
        $query->setProjection('full');
        $query->setOrderby('starttime');
        $query->setStartMin(date('Y-m-d 00:00:00'));
        $query->setStartMax(date('Y-m-d 23:59:59'));

        // echo date('Y-m-d G:i:s');

        $timestr = '2012-01-19T22:00:00.000-08:00';
        $date = new DateTime($timestr);

        // var_dump($date->format('d F  Y: H:i'));

        $eventFeed = $gdataCal->getCalendarEventFeed($query);
        echo "<ul>";
        echo "<li>";
        foreach ($eventFeed as $event) {

                echo "<ul>";

                foreach ($event->when as $when) {

                        echo "<li>Title: " . $event->title->text . "</li>";
                        echo "<li>Location: ". $event->where[0]."</li>";
                        echo "<li>Content: ".$event->content."</li>" ;
                        $startDateTime = new DateTime($when->startTime);
                        $endDateTime = new DateTime($when->endTime);
                        echo "<li>Starts: " . $startDateTime->format('d F  Y: H:i'). "</li>";
                        echo "<li>Ends: " . $endDateTime->format('d F  Y: H:i'). "</li>";
                }

                echo "</ul>";
                echo "</li>";
      }

      echo "</ul>";

抱歉,代码混乱-我仍在处理未解决的部分。但是事件检索正在工作......

这是输出!

Title: Test event 4
Location: International
Content: Testing Email
Starts: 06 June 2013: 21:00
Ends: 06 June 2013: 22:00
4

1 回答 1

0

我刚刚通过本机客户端 PHP API 找到了解决方案

foreach ($events->getItems() as $event) {
    echo $event->getStart()->getDateTime();
}
于 2014-04-30T11:23:12.603 回答