0

Google 캘린더의 이벤트 목록을 웹 페이지에 넣어야합니다. 내가 필요로하는 목록은 Google 캘린더 자체에서 볼 수 있습니다. 일정 목록의 일정 목록 (일정 유형)으로 여러 캘린더의 콘텐츠가 서로 다른 색으로 표시됩니다. Google 캘린더에서이 목록을 Iframe에 넣을 수도 있지만 문제는이 목록을 보려면 사용자가 로그인해야합니다. 그렇지 않으면 내가 할 수없는 일을 캘린더 공개로 설정해야합니다.PHP로 Google 캘린더의 목록보기를 만드는 방법

이전에는 캘린더 전용 URL이 있었지만 현재는 iCal 피드 만 개인적으로 사용할 수 있습니다.

그래서 나는 그런 목록을 만드는 PHP 스크립트를 찾으려고 노력했다. 놀랍게도 나는별로 찾지 못했습니다. 대부분의 페이지는 공개 캘린더를 사용하거나 연결하는 방법에 대한 기본적인 예제 일뿐입니다. 여기서는 Google의 PHP 예제 인 https://developers.google.com/google-apps/calendar/quickstart/php#step_3_set_up_the_sample 과 같이 작동하지만 그게 내가 필요로하는 것과는 아주 멀리 떨어져 있습니다. 포맷 할 수있는 html 출력과 3 가지 다른 캘린더의 데이터가 필요합니다.

내 머리를 조금 넘기는데 어딘가 다른 사람이 전에 다른 일을 한 것 같습니까? 아니면 그냥 슬리브에서 떨쳐 낼 수 있습니까? 또는 더 좋은 제안이 있습니까?

또 다른 시도는 phantomjs를 사용하여 캘린더에 로그인하고 스크린 샷을 만들었지 만 웹 서버에서는 실행할 수 없습니다.

+0

감안할 때 통찰력, ... 그 날이 천천히 반복 단계에서 솔루션에 도달입니다, 내가 생각 지나치게 복잡하다 목록보기 (Google 캘린더의 '일정'보기와 유사) # 560] (https://github.com/fullcalendar/fullcalendar/issues/560) 도움이 될 수 있습니다. – Teyam

답변

0

아래 코드를 사용하여 일정보기를 모방했습니다.

나를 위해 몇 가지 문제가있었습니다. 즉 하루 종일 이벤트의 종료일과 그 다음 날인 하루 종일 이벤트를 반환하는 비 종일 이벤트와 종일 만 날짜입니다. [-

코드는

<html> 
    <head> 
    <style type="text/css"> 
     html { 
     font-family: sans-serif; 
     } 
     .trips { 
     color:red; 
     } 
     .courses { 
     color:blue; 
     } 
     .canceled { 
     color:orange; 
     text-decoration:line-through; 
     } 
     .marketing { 
     color:green; 
     } 
     tr>td { 
     vertical-align: top; 
     } 
     td.time { 
     width: 110px; 
     } 
     td.date { 
     width: 140px; 
     } 
     td.summary { 
     font-weight:bold; 
     } 
     td { 
     padding: 0 0 5px 0; 
     border-bottom: 2px solid grey; 
     } 
     td td { 
     border-bottom: none; 
     } 
     span.location { 
     font-weight:normal; 
     color:black; 
     margin-left:10px; 
     } 
    </style> 
    </head> 
    <body> 
    [html comment broken up, to keep syntax highlighting in the php below] 
    < ! -- // having a html comment around php keeps php debug output from echo, print_r etc in source view 
<?php 
    /****************** 
    * list calendar-events mimicking the google calendar agenda-view 
    * allows to show a non-public calendars to non-authenticated clients 
    * code fragments from 
    * http://www.daimto.com/google-calendar-api-with-php-service-account/ 
    * https://developers.google.com/google-apps/calendar/v3/reference/events/list#response 
    * https://developers.google.com/google-apps/calendar/quickstart/php#step_3_set_up_the_sample 
    ******************/ 
    require_once 'google-api-php-client/src/Google/autoload.php'; 
    session_start(); 
    $Email_address = '[email protected]'; 
    // for security reasons, keep key_file outside webroot: 
    $key_file_location = '/home/t...../...41.p12'; 
    $client = new Google_Client(); 
    $client-> setApplicationName("K...."); 
    $key = file_get_contents($key_file_location); 
    // the calendars to query 
    $calendars = array(
    'trips' => '[email protected]', 
    'courses' => '[email protected]', 
    'canceled' => '[email protected]', 
); 
    $agenda = array(); // the merged result from all calendarsi 
    $maxResults = 15; // no. of results to get (per calendar) 
    $firstDate = new DateTime(); // the date from which on we want the agenda 
    $firstDate->setTime(0,0,0); // date "without" time, we think in full days only 
    // $firstDate->modify('+2 days'); // testing other start-dates 
    setlocale (LC_ALL, 'de_DE'); // to get weekdays & monthnames correct 
    $scopes ="https://www.googleapis.com/auth/calendar.readonly"; 
    $cred = new Google_Auth_AssertionCredentials(
    $Email_address, 
    array($scopes), 
    $key 
); 
    $client->setAssertionCredentials($cred); 
    if($client->getAuth()->isAccessTokenExpired()) { 
    $client->getAuth()->refreshTokenWithAssertion($cred); 
    } 
    $service = new Google_Service_Calendar($client); 

    foreach($calendars as $cal_name => $cal_id) { 
    // get the dates from each calendar 
    $calendar_res = $service->calendars->get($cal_id); 
    $optParams = array(
     'maxResults' => $maxResults, 
     'orderBy' => 'startTime', 
     'singleEvents' => true, 
     'timeMin' => $firstDate->format('c') 
    ); 
    $events = $service->events->listEvents($calendar_res->id, $optParams); 

    foreach ($events->getItems() as $event) { 
     $startDate = new DateTime(); 
     $endDate = new DateTime(); 
     // full-day events use 'date', others 'dateTime' so we need to treat separately: 
     if(isset($event->start->date)){ 
     // it's a full day event, only a date is given 
     $startDate->setTimestamp(strtotime($event->start->date)); 
     $endDate->setTimestamp(strtotime($event->end->date)); 
     // full-day end-date is returned by google as the next day (midnight), 
     // correct this for our display: 
     $endDate->sub(new DateInterval('P1D')); 
     // remove times, they would contain data from the last processed non- full-day event 
     // also, we will test ifset to recognize full- against non- full-day events 
     unset($startTime); 
     unset($endTime); 
     }else{ 
     // it's a non-full day, having start/end dates AND times 
     $startDate->setTimestamp(strtotime($event->start->dateTime)); 
     $endDate->setTimestamp(strtotime($event->end->dateTime)); 
     // extract times 
     $startTime = $startDate->format('G:i'); 
     $endTime = $endDate->format('G:i'); 
     // set times to zero, so date comparison works correctly 
     $startDate->setTime(0,0,0); 
     $endDate->setTime(0,0,0); 
     } 

     // for every day of the event, make an entry in the agenda 
     $currDate = $startDate; // the date we are about to add an entry to 
     while ($endDate >= $currDate){ 
     // don't add entries that are before our first wanted date 
     if ($currDate >= $firstDate){ 
      if (isset ($startTime)){ 
      $time = $startTime . " - " . $endTime; 
      }else{ 
      $time = "Ganztägig"; 
      }; 
      // we save the date in a way so the agenda-array can later be sorted by it 
      $agenda[$currDate->format('Y-m-d')][] = 
      array(
       'cal' => $cal_name, 
       'summary' => $event->getSummary(), 
       'location' => $event->getLocation(), 
       'start' => $startDate->format('Y-m-d') . " - " . $startTime, 
       'end' => $endDate->format('Y-m-d') . " - " . $endTime, 
       'time' => $time 
      ); 
     }; 
     // go to next day 
     $currDate->modify('+1 day'); 
     }; 
    } 
    } 

    // the agenda-array is not yet sorted, events were added by calendar by date, not just by date 
    ksort ($agenda); // sort by key (date) 
    //var_dump($agenda); 
    //print_r($agenda); 
?> 
    // end of html comment around php (keeps debug output in source view) --> 

<? 
    //output 
    echo " <table>"; 
    foreach ($agenda as $aDate => $events){ 
    // a row for every date 
    echo "\n  <tr>"; 
    echo "\n  <td class=\"date\">" . strftime('%a %e. %B', strtotime($aDate)) . "</td>"; 
    echo "\n  <td>"; 
    // a table of events for every day 
    echo "\n   <table >"; 
    foreach ($events as $aEvent){ 
     // a row for every event 
     echo "\n   <tr>"; 
     echo "\n    <td class=\"time\">" . $aEvent['time'] ."</td>"; 
     echo "\n    <td class=\"" . $aEvent['cal'] . " summary\">" . $aEvent['summary']; 
     echo "\n    <span class=\"location\">" . $aEvent['location'] . "</span>"; 
     echo "\n    </td>"; 
     echo "\n   </tr>"; 
    }; 
    echo "\n   </table>"; 
    echo "\n  </td>"; 
    echo "\n  </tr>"; 
    }; 
    echo "\n <table>\n"; 
?> 
    </body> 
</html> 
함께 GitHub의에서이 글의 샘플 코드와/또는 제안