Google Calendar API - PHP - 搜索单个活动

时间:2016-04-18 11:05:55

标签: php google-calendar-api

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

<?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{
}

?>

1 个答案:

答案 0 :(得分:0)

查看events.list

的文档
  

q

     

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

我的php有点生疏,但我猜它可能是这样的

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

您可以尝试在底部试试我来调试它。

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