简体   繁体   中英

Google Calendar API - PHP - Search for single event

I want to search and output a single event from my calendar. The calendar event contains a unique string, say numbers "12345678" (= the variable "string"). How can I define this search in the following code?

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

?>

Check the documentation for events.list

q

Free text search terms to find events that match these terms in any field, except for extended properties. Optional.

My php is a little rusty but I can guess it might be something like this

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

The you can try the try me at the bottom to debug it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM