如何在PHP中创建事件后检索Google Calendar事件ID

时间:2016-09-20 17:10:46

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

我有一个PHP文件,可以从我的CRM发布的数据中成功地在我的Google日历中创建一个事件,但是,我找不到任何关于它如何能够检索它刚创建的事件的id的文档返回JSON数据字符串。

这是我当前的PHP文件。

<?php

require_once __DIR__ . '/google-api-php-client/src/Google/autoload.php';

$summary = $_POST["summary"];
$location = $_POST["location"];
$description = $_POST["description"];
$startdatetime = $_POST["startdatetime"];
$enddatetime = $_POST["enddatetime"];



$client_email = 'xxxxxxxx@xxxxxxxx.iam.gserviceaccount.com';
$private_key = file_get_contents('xxxxxx.p12');
$scopes = array('https://www.googleapis.com/auth/calendar');
$user_to_impersonate = 'xxxxxxx@xxxxxxxxxxx.com';
$credentials = new Google_Auth_AssertionCredentials(
    $client_email,
    $scopes,
    $private_key,
    'notasecret',                                 // Default P12 password
    'http://oauth.net/grant_type/jwt/1.0/bearer', // Default grant type
    $user_to_impersonate
);
$client = new Google_Client();
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion();
}
$event = new Google_Service_Calendar_Event(array(
  'summary' => $summary,
  'location' => $location,
  'description' => $description,
  'start' => array(
    'dateTime' => $startdatetime,
    'timeZone' => 'America/Los_Angeles',
  ),
  'end' => array(
    'dateTime' => $enddatetime,
    'timeZone' => 'America/Los_Angeles',
  ),
  'reminders' => array(
    'useDefault' => FALSE,
  ),
));
$service = new Google_Service_Calendar($client);
$calendarId = 'southbay@unlikelylegendsmedia.com';
$event = $service->events->insert($calendarId, $event);

1 个答案:

答案 0 :(得分:0)

如果您知道相应的calendarID,则可以使用Events: list

通过发送带有calendarId参数的HTTP请求,您可以检索eventId。请求如:

GET https://www.googleapis.com/calendar/v3/calendars/calendarId/events

如果请求成功,此方法可能会返回一个响应正文,其中包含单个一次性事件和重复事件的实例。

示例PHP代码:

$events = $service->events->listEvents('primary');

while(true) {
  foreach ($events->getItems() as $event) {
    echo $event->getSummary();
  }
  $pageToken = $events->getNextPageToken();
  if ($pageToken) {
    $optParams = array('pageToken' => $pageToken);
    $events = $service->events->listEvents('primary', $optParams);
  } else {
    break;
  }
}