活动开始时间和结束时间未显示 - 使用google-api-php-client的Google日历

时间:2013-06-06 16:27:48

标签: php google-calendar-api google-api-php-client

<?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和     时间结束。还有办法只显示今天的事件吗?最终我想在startTime和我的网站上显示一个流行音乐     当前时间是一样的。那么有人可以帮助我获得StartTime吗?

  
     

我看过这里:   https://developers.google.com/google-apps/calendar/v3/reference/events/list   但它没有说明如何获得startTime和结束时间。这是   真的很必要。   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

1 个答案:

答案 0 :(得分:0)

我刚通过本机客户端PHP API

找到了解决方案
foreach ($events->getItems() as $event) {
    echo $event->getStart()->getDateTime();
}