Set google event reminders with api for Google Calendar from php - php

i have a script php for creating a google calendar event.
the script work correctly but the function to set the date wrong.
the script his
[...initial statements...]
$event = new Google_Event();
$event->setSummary("test");
$event->setLocation("location");
$start = new Google_EventDateTime();
$start->setDateTime(2014-05-01T10:00:00.000-07:00);
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDateTime(2014-05-01T10:00:00.000-08:00);
$event->setEnd($end);
$reminder = new Google_EventReminders();
$reminder->setUseDefault(false);
$overrides = array("method"=> "popup","minutes" => "15");
$reminder->setOverrides(array($overrides));
$event->setReminders($reminder);
$createdEvent = $service->events->insert('primary', $event);
[...]
in event created i ha have two reminders
email 10 minutes and popup 10 minutes

Here is how I got mine to work :)
$event = new Google_Event(); //note in the API examples it calls Event(). Apparently, they changed it and didn't update examples.
$event->setSummary($task);
$event->setColorId('4');
$event->setDescription($type);
$event->setLocation($type);
$start = new Google_EventDateTime(); //note in the API examples it calls EventDateTime().
$start->setDateTime($date1);
$event->setStart($start);
$end = new Google_EventDateTime(); //note in the API examples it calls EventDateTime().
$end->setDateTime($date2);
$event->setEnd($end);
$attendee1 = new Google_EventAttendee(); //note in the API examples it calls EventAttendee().
$attendee1->setEmail('myemail#gmail.com'); //make sure you actually put an email address in here
$reminderI = new Google_EventReminder();
$reminderI->setMethod('email');
$reminderI->setMinutes('25');
$reminder = new Google_EventReminders();
$reminder->setUseDefault('false');
$reminder->setOverrides(array($reminderI));
$event->setReminders($reminder);

I spent a few minutes and figure it out, try this:
$reminders = new Google_Service_Calendar_EventReminders();
$reminders->setUseDefault(false);
$reminder = new Google_Service_Calendar_EventReminder();
$method = empty($v['method']) ? 'email' : $v['method'];
$minute = empty($v['minute']) ? '10' : $v['minute'];
$reminder->setMethod($method);
$reminder->setMinutes($minute);
$reminders->setOverrides(array($reminder));
$targetEvent->setReminders($reminders);
Notice here $reminders->setOverrides(array($reminder)); we need to put the $reminder in an array while calling $reminders->setOverrides
I am using the API V3, and I guess you're using V2. Anyway, it should be quite similar.
Hope this helps.

I think the problem is here:
$overrides = array("method"=> "popup","minutes" => "15");
You are essentially wrapping an array inside an array in here:
setOverrides(array($overrides));
The override should instead be an object of type Google_Service_Calendar_EventReminder.

Related

Want to create Google Clanedar event which repeats yearly

I successfully managed to add event to google calendar through php api client, but what i want is to make that event repeat every year , such as company anniversay etc, i didn't find that in the google documentation also ,so what should be added while creating event for repeatation
There is documentation for recurring event
$event = new Google_Service_Calendar_Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2011-06-03T10:00:00.000-07:00');
$start->setTimeZone('America/Los_Angeles');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2011-06-03T10:25:00.000-07:00');
$end->setTimeZone('America/Los_Angeles');
$event->setEnd($end);
$event->setRecurrence(array('RRULE:FREQ=YEARLY;UNTIL=20110701T170000Z'));
$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setEmail('attendeeEmail');
// ...
$attendees = array($attendee1,
// ...
);
$event->attendees = $attendees;
$recurringEvent = $service->events->insert('primary', $event);
echo $recurringEvent->getId();

How to insert event into a private Google Calendar with PHP?

I'm trying to add new events into my private Google Calendar, but I get (403) Forbidden message. I've searched the internet but I found no answer to my issue.
My scope is set to read/write, and the credentials should be ok, because I can read out all of my events so the connection works fine.
Here's the code:
$service_account = $this->service_account;
$json_key = json_decode(file_get_contents($this->json_url));
$scopes = 'https://www.googleapis.com/auth/calendar';
$application_name = 'Calendar';
$client = new Google_Client();
$client->setApplicationName($application_name);
$cred = new Google_Auth_AssertionCredentials($service_account, array($scopes), $json_key->private_key);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$google_service = new Google_Service_Calendar($client);
$calendar_ids = $this->calendar_ids;
$event = new Google_Service_Calendar_Event();
$event->setSummary($service);
$event->setDescription($user);
$event->setLocation($location);
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime($this->sqlDateToGcalDate($start_date));
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime($this->sqlDateToGcalDate($end_date));
$event->setEnd($end);
$google_service->events->insert($calendar_ids[$steed], $event);
$google_service->events->insert($calendar_ids[$location], $event);

How to update google calendar event datetime using API V3

I can successfully update the title of the events using Google API V3, but once I include the datetime, I get internal server error. Here's the snippet of my code:
$event = $service->events->get($calendar_id,$event_id);
$event->setSummary($notes);
$service->events->update($calendar_id,$event->getId(),$event);
The above code works well since I'm only updating the title. I'm struggling with the code below:
$event = $service->events->get($calendar_id,$event_id);
$event->setSummary($notes);
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2014-07-22T20:00:00+08:00');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2014-07-22T21:00:00+08:00');
$event->setEnd($end);
$service->events->update($calendar_id,$event->getId(),$event);
I'm not sure if I have the correct format for the datetime. Any advice? Thanks!
// Cambio de fecha de evento
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2021-09-01T14:00:00');
$end->setTimeZone('America/Mexico_City');
$event->setEnd($end);
$Start = new Google_Service_Calendar_EventDateTime();
$Start->setDateTime('2021-09-01T12:00:00');
$Start->setTimeZone('America/Mexico_City');
$event->setStart($Start);
$updatedEvent = $service->events->update(CalendarID, $event->getId(), $event);
You probably want to do $event->setEnd($end); instead of $event->setStart($end);

Google Calendar PHP API, what am I doing wrong?

<?php
session_start();
require_once "Google/Client.php";
require_once "Google/Service/Calendar.php";
$apiClient = new Google_Client();
$apiClient->setApplicationName("My Application");
$apiClient->setDeveloperKey("API_KEY");
$service = new Google_Service_Calendar($apiClient);
$event = new Google_Service_Calendar_Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2011-06-03T10:00:00.000-07:00');
$start->setTimeZone('America/Los_Angeles');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2011-06-03T10:25:00.000-07:00');
$end->setTimeZone('America/Los_Angeles');
$event->setEnd($end);
$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setEmail('EMAIL');
$attendees = array($attendee1,
// ...
);
$event->setAttendees($attendees);
$recurringEvent = $service->events->insert('primary', $event);
echo $recurringEvent->getId();
?>
Everything compiles correctly until I execute $service->events->insert? am I authenticating wrong? I have been trying this for days. Alls I'm trying to do is simply authenticate, and create a calendar event. (I don't need multi-user auth, just one admin user).
Help is appreciated!

Google Calendar API batch request PHP

i am trying to send a bunch of events via an Batch Request to Google Calendar.
But i cant figur out how to do. https://developers.google.com/google-apps/calendar/batch doesnt help me.
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_CalendarService.php';
$client = new Google_Client();
$client->setUseBatch(true);
$batch = new Google_BatchRequest();
$uri = 'http://www.google.com/calendar/feeds/default/private/full/batch';
$batchContent = file_get_contents('xxxxx/google-api-php-client/batch.xml');
$batch->add($batchContent);
batch.xml contains 2 -items.
Thats all so far. But nothing happened.
I also have tried
$batch->execute()
But thats throws error without message.
My question: How to send a Batch via PHP to Google Calendar ?
I'm using the latest version of Google APIs Client Library for PHP (Google Calendar v.3). I use batch operations for pushing instructor lessons to Google Calendar. Here is my example of code for you (multipleInsert function). Good luck!
<?php
require_once(APPPATH . 'libraries/Google/Client.php');
require_once(APPPATH . 'libraries/Google/Http/Batch.php');
require_once(APPPATH . 'libraries/Google/Service/Calendar.php');
class My_google_calendar
{
...
/** Add single Event for Student */
function addEvent($lesson, $instructor, $return_request = false, $enable_attendees = false)
{
$calendar = $this->getGoogleCalendar(); // get calendar service variable
$lesson_from = date(DATE_RFC3339, $lesson->from);
$lesson_to = date(DATE_RFC3339, $lesson->from + $lesson->duration);
$event = new Google_Service_Calendar_Event();
$event->setSummary('Lesson with student: '$lesson->student_full_name);
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime($lesson_from);
$start->setTimeZone($this->getGoogleCalendarTimeZone());
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime($lesson_to);
$end->setTimeZone($this->getGoogleCalendarTimeZone());
$event->setEnd($end);
$event->setColorId(4);
$description = "...";
$event->setDescription($description);
if (isset($student->email) && $enable_attendees) {
$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setResponseStatus('needsAction');
$attendee1->setEmail($student->email);
$attendees = array($attendee1);
$event->setAttendees($attendees);
}
$createdEvent = $this->calendar->events->insert($this->calendar_id, $event, array('fields' => 'id'));
return $return_request ? $createdEvent : $createdEvent->getId();
}
/** Push group of events to the Calendar */
function multipleInsert ($lessons, $instructor)
{
$this->use_batch = true;
$this->client->setUseBatch($this->use_batch);
$batch = new Google_Http_Batch($this->client);
$optParams = array('fields' => 'status,updates');
foreach($lessons as $time => $lesson) {
$lesson = array_shift($group['lessons']);
$req = $this->addEvent($lesson, $instructor, true);
$batch->add($req, $time);
}
}
$results = $batch->execute();
return $results;
}
}
in the newer versions, you should use
$batch = $service->createBatch();
instead of
$batch = new Google_Http_Batch($this->client);
reference

Categories