谷歌日历API-PHP-搜索单个事件


Google Calendar API - PHP - Search for single event

我想从日历中搜索并输出一个事件。日历事件包含一个唯一的字符串,比如数字"12345678"(=变量"字符串")。如何在以下代码中定义此搜索?

<?php
require_once "../google-api/src/Google/autoload.php";
require_once "../google-api/src/Google/Client.php";
require_once "../google-api/src/Google/Service/Calendar.php";
if (isset($_GET['string']) {
$string = $_GET['id'];

$client_id = '[concealed]'; //client of the service account
$Email_address = '[concealed]'; //email id of the service account
$key_file_location = '../google-api/API_Project-[concealed].p12'; //private p12
$client = new Google_Client();
$client->setApplicationName("API Project"); //name of the application
$key = file_get_contents($key_file_location);
// seproate additional scopes with a comma
$scopes ="https://www.googleapis.com/auth/calendar";
$creds = new Google_Auth_AssertionCredentials(
 $Email_address,
 array($scopes),
 $key
 );
$client->setAssertionCredentials($creds);
if($client->getAuth()->isAccessTokenExpired()) {
 $client->getAuth()->refreshTokenWithAssertion($creds);
}
$service = new Google_Service_Calendar($client);
$calendarList = $service->calendarList->listCalendarList();
$calendarId = '[concealed]'; // Replace this with your calendar ID
$optParams = array(
  'singleEvents' => TRUE,
);
try {
    $events = $service->events->listEvents($calendarId, $optParams);
} catch( Exception $e ) {
    print_r( $e );
}
while(true) {
    try {
        foreach ($events->getItems() as $event) {
            $name = $event->getSummary();
        }
    } catch( Exception $e ) {
        print_r( $e );
    }
    $pageToken = $events->getNextPageToken();
    if ($pageToken) {
        $optParams['pageToken'] = $pageToken;
        $events = $service->events->listEvents($calendarId, $optParams);
    } else {
        break;
    }
}
}
else{
}
?>

查看文档中的事件列表

q

自由文本搜索项可在任何字段中查找与这些项匹配的事件,扩展属性除外。可选择的

我的php有点生疏,但我可以猜测它可能是类似于这个的东西

$optParams = array('pageToken' => $pageToken, 'q' => '12345678');

你可以试试底部的try me来调试它。

https://www.googleapis.com/calendar/v3/calendars/primary/events?q=hair&key={YOUR_API_KEY}