2013-03-14 106 views
1

如何將事件插入到Google日曆中?使用php插入事件到Google日曆中

我使用本指南: https://developers.google.com/google-apps/calendar/v3/reference/events/insert

實例部分,有這樣的PHP代碼:

$event = new Event(); 
$event->setSummary('Appointment'); 
$event->setLocation('Somewhere'); 
$start = new EventDateTime(); 
$start->setDateTime('2011-06-03T10:00:00.000-07:00'); 
$event->setStart($start); 
$end = new EventDateTime(); 
$end->setDateTime('2011-06-03T10:25:00.000-07:00'); 
$event->setEnd($end); 
$attendee1 = new EventAttendee(); 
$attendee1->setEmail('attendeeEmail'); 
// ... 
$attendees = array($attendee1, 
        // ... 
       ); 
$event->attendees = $attendees; 
$createdEvent = $service->events->insert('primary', $event); 

echo $createdEvent->getId(); 

但是,這給了我一個致命的錯誤,因爲$服務是不確定的。 任何人都可以告訴我如何初始化$服務,並使這個東西工作?

回答

0

它在this頁描述(右上角切換到PHP源)。代碼複製如下。

的src/config.php文件

global $apiConfig; 
$apiConfig = array(
    // Site name to show in Google's OAuth authentication screen 
    'site_name' => 'www.example.org', 

    // OAuth2 Setting, you can get these keys on the API Access tab on 
    // the Google APIs Console 
    'oauth2_client_id' => 'YOUR_CLIENT_ID', 
    'oauth2_client_secret' => 'YOUR_CLIENT_SECRET', 
    'oauth2_redirect_uri' => 'YOUR_REDIRECT_URL', 

    // The developer key; you get this from the Google APIs Console 
    'developer_key' => 'YOUR_DEVELOPER_KEY', 

    // Which Authentication, Storage and HTTP IO classes to use. 
    'authClass' => 'apiOAuth2', 

    // Definition of service specific values like scopes, OAuth token URLs, etc 
    'services' => array(
     'calendar' => array('scope' => 'https://www.googleapis.com/auth/calendar'), 
) 
); 

腳本

session_start(); 

require_once "../src/apiClient.php"; 
require_once "../src/contrib/apiCalendarService.php"; 

$apiClient = new apiClient(); 
$apiClient->setUseObjects(true); 
$service = new apiCalendarService($apiClient); 

if (isset($_SESSION['oauth_access_token'])) { 
    $apiClient->setAccessToken($_SESSION['oauth_access_token']); 
} else { 
    $token = $apiClient->authenticate(); 
    $_SESSION['oauth_access_token'] = $token; 
} 
+0

$ apiClient =新apiClient(); class apiClient在Google Api中不存在 ...以及我可以找到的所有文檔都是混合版本和構建版本... 也許我應該使用 $ apiClient = new Google_Client(); 取而代之,但因爲$ apiClient中沒有$ auth對象而中斷了,並且我收到錯誤消息「無法解碼令牌的json」。 – 2013-03-14 15:04:32

+0

我使用 http://code.google.com/p/google-api-php-client/downloads/list – 2013-03-14 15:14:48

+1

你看過calendar/simple.php中的例子嗎? https://code.google.com/p/google-api-php-client/source/browse/trunk/examples/calendar/simple.php – 2013-03-14 15:32:17

3

在谷歌API V3 PHP客戶端的最新版本,你應該使用

$event = new Google_Event(); 

代替

$event = new Event(); 

,還可以使用

$start = new Google_EventDateTime(); 

,而不是

$start = new EventDateTime(); 

當然的需要定義不同的,用這一個:

require_once '../src/Google_Client.php'; 
require_once '../src/contrib/Google_CalendarService.php'; 
+0

這又改變了嗎?這不再起作用 – terrorfall 2014-09-08 15:43:24

0

我的腳本運行良好所以, 機智^ h插入,更改和刪除谷歌事件月份牌

<!DOCTYPE html> 
 
<!-- 
 
*********** GOOGLE CALENDAR **************************** 
 
Author: Vanderlei Bailo 
 
E-mail: [email protected] 
 
Script: insert, change and delete Google Calenda event 
 
--> 
 
<html> 
 
    <head> 
 
     <title>GOOGLE CALENDAR - insert, change and delete Google Calenda event</title> 
 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
 
     <meta charset="UTF-8"> 
 
     <style> 
 
      body{ 
 
       margin: 0; 
 
       width: 100%; 
 
       font-family: Verdana, Arial; 
 
      } 
 
      #centro{ 
 
       width: 780px; 
 
       margin: auto; 
 
      } 
 
      .calendario{ 
 
       position: relative; 
 
       width: 800px; 
 
       height: 600px; 
 
       margin-left:-390px; 
 
       left: 50%; 
 
       float: left; 
 
       -webkit-border-radius: 5px; 
 
       -moz-border-radius: 5px; 
 
       border-radius: 5px; 
 
      } 
 
      #datahora{ 
 
       width: 250px; 
 
       float: left; 
 
      } 
 
      #cento{ 
 
       width: 780px; 
 
       float: left; 
 
      } 
 
      #centro .primo{ 
 
       width: 100%; 
 
       background-color: #E3E9FF; 
 
       padding: 10px; 
 
       margin: 50px 0; 
 
       float: left; 
 
       -webkit-border-radius: 5px; 
 
       -moz-border-radius: 5px; 
 
       border-radius: 5px; 
 
      } 
 
      label { 
 
       width: 780px; 
 
       margin: 5px 5px 0; 
 
       float: left; 
 
       padding-top: 10px; 
 
      } 
 

 
      input{ 
 
       margin: 5px; 
 
       float: left; 
 
       padding: 5px 10px; 
 
       -webkit-border-radius: 5px; 
 
       -moz-border-radius: 5px; 
 
       border-radius: 5px; 
 
       border: 1px #CCC solid; 
 
      } 
 
      input[type="text"]{ 
 
       width: 750px; 
 
      } 
 
      input[type="date"]{ 
 
       width: 125px; 
 
      } 
 
      input[type="time"]{ 
 
       width: 70px; 
 
      } 
 
      input[type="submit"]{ 
 

 
      } 
 

 
      input:focus{ 
 
       border: 1px #cc0000 solid; 
 
       box-shadow: 0 0 5px #cc0000; 
 
      } 
 
      .btn { 
 
       background: #3498db; 
 
       background-image: -webkit-linear-gradient(top, #3498db, #2980b9); 
 
       background-image: -moz-linear-gradient(top, #3498db, #2980b9); 
 
       background-image: -ms-linear-gradient(top, #3498db, #2980b9); 
 
       background-image: -o-linear-gradient(top, #3498db, #2980b9); 
 
       background-image: linear-gradient(to bottom, #3498db, #2980b9); 
 
       -webkit-border-radius: 5; 
 
       -moz-border-radius: 5; 
 
       border-radius: 5px; 
 
       font-family: Arial; 
 
       color: #ffffff; 
 
       font-size: 20px; 
 
       padding: 10px 20px 10px 20px; 
 
       text-decoration: none; 
 
       cursor: pointer; 
 
      } 
 

 
      .btn:hover { 
 
       background: #3cb0fd; 
 
       background-image: -webkit-linear-gradient(top, #3cb0fd, #3498db); 
 
       background-image: -moz-linear-gradient(top, #3cb0fd, #3498db); 
 
       background-image: -ms-linear-gradient(top, #3cb0fd, #3498db); 
 
       background-image: -o-linear-gradient(top, #3cb0fd, #3498db); 
 
       background-image: linear-gradient(to bottom, #3cb0fd, #3498db); 
 
       text-decoration: none; 
 
      } 
 

 
     </style> 
 
    </head> 
 
    <body> 
 

 
     <?php 
 
     session_start(); 
 
     require 'google-api-php-client-master/src/Google/autoload.php'; 
 
     require_once 'google-api-php-client-master/src/Google/Client.php'; 
 
     require_once 'google-api-php-client-master/src/Google/Service/Calendar.php'; 
 

 
     $client_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com'; //change this 
 
     $Email_address = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.gserviceaccount.com'; //change this 
 
     $key_file_location = 'xxxxxxxxxxxxxxxxxxxxxxxx.p12'; //change this 
 
     $client = new Google_Client(); 
 
     $client->setApplicationName("Client_Library_Examples"); 
 
     $key = file_get_contents($key_file_location); 
 

 

 
     $scopes = "https://www.googleapis.com/auth/calendar"; 
 
     $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); 
 

 
     $calendarList = $service->calendarList->listCalendarList(); 
 
     while (true) { 
 
      foreach ($calendarList->getItems() as $calendarListEntry) { 
 
       echo "<a href='Oauth2.php?type=event&id=" . $calendarListEntry->id . " '>" . $calendarListEntry->getSummary() . "</a><br>\n"; 
 
      } 
 
      $pageToken = $calendarList->getNextPageToken(); 
 
      if ($pageToken) { 
 
       $optParams = array('pageToken' => $pageToken); 
 
       $calendarList = $service->calendarList->listCalendarList($optParams); 
 
      } else { 
 
       break; 
 
      } 
 
     } 
 

 

 
     if ($_POST) { 
 
      
 
      $Summary = $_POST['Summary']; 
 
      $Location = $_POST['Location']; 
 
      $DateStart = $_POST['DateStart']; 
 
      $TimeStart = $_POST['TimeStart']; 
 
      $DateEnd = $_POST['DateEnd']; 
 
      $TimeEnd = $_POST['TimeEnd']; 
 
      $acao = $_POST['acao']; 
 

 

 

 
      if ($acao == 'Inserire') { 
 
       //--------------- trying to insert EVENT --------------- 
 
       $event = new Google_Service_Calendar_Event(); 
 
       $event->setSummary($Summary); 
 
       $event->setLocation($Location); 
 
       $start = new Google_Service_Calendar_EventDateTime(); 
 
       $datatimeI = geratime(DataIT2DB($DateStart), $TimeStart); 
 

 
       $start->setDateTime($datatimeI); 
 
       $event->setStart($start); 
 
       $end = new Google_Service_Calendar_EventDateTime(); 
 
       $datatimeF = geratime(DataIT2DB($DateEnd), $TimeEnd); 
 

 
       $end->setDateTime($datatimeF); 
 
       $event->setEnd($end); 
 
       $attendee1 = new Google_Service_Calendar_EventAttendee(); 
 
       $attendee1->setEmail('[email protected]'); 
 
       $attendees = array($attendee1); 
 
       $event->attendees = $attendees; 
 
       $createdEvent = $service->events->insert('primary', $event); 
 
       $_SESSION['eventID'] = $createdEvent->getId(); 
 
      } else if ($acao == 'Cancellare') { 
 
       //--------------- trying to del EVENT --------------- 
 
       $createdEvent = $service->events->delete('primary', $_SESSION['eventID']); 
 
      } else if ($acao == 'Aggiornare') { 
 
       //--------------- trying to update EVENT --------------- 
 

 
       $rule = $service->events->get('primary', $_SESSION['eventID']); 
 

 

 
       $event = new Google_Service_Calendar_Event(); 
 
       $event->setSummary($Summary); 
 
       $event->setLocation($Location); 
 
       $start = new Google_Service_Calendar_EventDateTime(); 
 
       $datatimeI = geratime(DataIT2DB($DateStart), $TimeStart); 
 

 
       $start->setDateTime($datatimeI); 
 
       $event->setStart($start); 
 
       $end = new Google_Service_Calendar_EventDateTime(); 
 
       $datatimeF = geratime(DataIT2DB($DateEnd), $TimeEnd); 
 

 
       $end->setDateTime($datatimeF); 
 
       $event->setEnd($end); 
 
       $attendee1 = new Google_Service_Calendar_EventAttendee(); 
 
       $attendee1->setEmail('[email protected]'); //change this 
 
       $attendees = array($attendee1); 
 
       $event->attendees = $attendees; 
 

 
       $updatedRule = $service->events->update('primary', $rule->getId(), $event); 
 
      } 
 
     } 
 

 
     function DataIT2DB($datapega) { 
 
      if ($datapega) { 
 
       $data = explode('/', $datapega); 
 
       if (count($data) > 1) { 
 
        $datacerta = $data[2] . '-' . $data[1] . '-' . $data[0]; 
 
       } else { 
 
        $datacerta = $datapega; 
 
       } 
 
      } else { 
 
       $datacerta = $datapega; 
 
      } 
 
      return $datacerta; 
 
     } 
 

 
     function geratime($DateStart, $TimeStart) { 
 
      $dataHora = $DateStart . 'T' . $TimeStart . ':00.000+02:00'; //Fuso Rome 
 
      return $dataHora; 
 
     } 
 
     ?> 
 

 
     <div id="contenut" style="width: 100%; float: left;"> 
 
      <div id="centro"> 
 
       <div class="primo"> 
 
        <form name="adicionar" method="POST" action="#"> 
 
         ID evento: <?php echo (isset($_SESSION['eventID']) ? $_SESSION['eventID'] : ""); ?> 
 
         <input type="hidden" name="" value="<?php echo (isset($_SESSION['eventID']) ? $_SESSION['eventID'] : ""); ?>" /> 
 
         <input type="text" name="Summary" value="<?php echo (isset($_POST['Summary']) ? $_POST['Summary'] : ""); ?>" placeholder="Titolo"/> 
 
         <input type="text" name="Location" value="<?php echo (isset($_POST['Location']) ? $_POST['Location'] : ""); ?>" placeholder="Località"/> 
 
         <div id="datahora"> 
 
          <label>Data e ora di inizio</label> 
 
          <input type="date" name="DateStart" value="<?php echo (isset($_POST['DateStart']) ? $_POST['DateStart'] : ""); ?>" placeholder="GG/MM/AAAA"/> 
 
          <input type="time" name="TimeStart" value="<?php echo (isset($_POST['TimeStart']) ? $_POST['TimeStart'] : ""); ?>" placeholder="10:20"/> 
 
         </div> 
 
         <div id="datahora"> 
 
          <label>Data e ora di fine</label> 
 
          <input type="date" name="DateEnd" value="<?php echo (isset($_POST['DateEnd']) ? $_POST['DateEnd'] : ""); ?>" placeholder="GG/MM/AAAA"/> 
 
          <input type="time" name="TimeEnd" value="<?php echo (isset($_POST['TimeEnd']) ? $_POST['TimeEnd'] : ""); ?>" placeholder="10:20" /> 
 
         </div> 
 
         <div id="cento"> 
 
          <input class="btn" type="submit" value="Inserire" name="acao" /> 
 
          <input class="btn" type="submit" value="Cancellare" name="acao" /> 
 
          <input class="btn" type="submit" value="Aggiornare" name="acao" /> 
 
         </div> 
 

 
        </form> 
 
       </div> 
 
      </div> 
 
     </div> 
 
    </body> 
 
</html>

+0

什麼是$ key_file_location – 2017-03-07 09:02:40

-1
full code for calendar: 
this session has all 

<?php 
ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); 
error_reporting(E_ALL); 

require_once '../../src/Google_Client.php'; 
require_once '../../src/contrib/Google_CalendarService.php'; 
//require_once '../../src/vendor/autoload.php'; 
session_start(); 

print_r($_SESSION); 

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

// Visit https://code.google.com/apis/console?api=calendar to generate your 
// client id, client secret, and to register your redirect uri. 
// $client->setClientId('insert_your_oauth2_client_id'); 
// $client->setClientSecret('insert_your_oauth2_client_secret'); 
// $client->setRedirectUri('insert_your_oauth2_redirect_uri'); 
// $client->setDeveloperKey('insert_your_developer_key'); 
$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']); 
} 

if ($client->getAccessToken()) { 
    $calList = $cal->calendarList->listCalendarList(); 
    print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>"; 


$_SESSION['token'] = $client->getAccessToken(); 
} else { 
    $authUrl = $client->createAuthUrl(); 
    print "<a class='login' href='$authUrl'>Connect Me!</a>"; 
} 



?>