Home > Enterprise >  Google Calendar API - create event PHP
Google Calendar API - create event PHP

Time:12-03

I want to insert a new event into Google Calendar by using the Google Calendar API. And the problem is that I want to set the event status to "free" or "busy" based on the input datetime data.

enter image description here

How to change the event status to "Free"? When I create a new event, it will automatically set to "Busy". I couldn't figure it out on how to set to "Free" or "Busy".

How I insert a new event by using Google Calendar API.

$googleClient = new GoogleClient();
$slotCalendarService = GoogleClientCalendar::getSlotCalendar($googleClient->getClient());

$googleEvent = new Event();

$googleEventStart = new EventDateTime();
$googleEventStart->setTimeZone($attrs['timezone']);
$googleEventStart->setDateTime("2021-12-02T10:00:00");
$googleEvent->setStart($googleEventStart);

$googleEventEnd = new EventDateTime();
$googleEventEnd->setTimeZone($attrs['timezone']);
$googleEventEnd->setDateTime("2021-12-02T10:50:00");
$googleEvent->setEnd($googleEventEnd);

$googleEventAttendee = new EventAttendee();
$googleEventAttendee->email = empty($attrs['attendee']) ? '' : $attrs['attendee'];
$googleEvent->description = $description;
$googleEvent->summary = $remark;
$googleEvent->setAttendees($googleEventAttendee);
$slotCalendarService->getCalendarService()->events->insert($slotCalendarService->getCurrentCalendarId(), $googleEvent);

Base on the the code above, there is no param to set the event status to "free" or "busy". I also follow the document FreeBusy Google Calendar API and it does not have information on how to set event status to "free" or "busy".

CodePudding user response:

When you want to set to "free", please modify as follows.

From:

$googleEvent->setAttendees($googleEventAttendee);
$slotCalendarService->getCalendarService()->events->insert($slotCalendarService->getCurrentCalendarId(), $googleEvent);

To:

$googleEvent->setAttendees($googleEventAttendee);
$googleEvent->transparency = "transparent"; // Added
$slotCalendarService->getCalendarService()->events->insert($slotCalendarService->getCurrentCalendarId(), $googleEvent);
  • When you don't use $googleEvent->transparency = "transparent"; or you use $googleEvent->transparency = "opaque";, it's "busy".

Reference:

  • Related