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
Related
I have a custom form that i am trying to write the fields to Google Calendar using php.
When I submit my form and it writes to the database but i need to write to Google calendar as well.
I debug the code and it stops at the insert statement create event code
$createdEvent = $service->events->insert("junkpolice2060#gmail.com", $newEvent);
I hard coded the calendar id to see if it will go but no go every time It stops here.
please help here is the full code
require_once "/var/www/www.junkpolice.com/html/components/com_osservicesbooking/google- api-php-client-master/src/Google/Client.php";
require_once "/var/www/www.junkpolice.com/html/components/com_osservicesbooking/google-api-php-client-master/src/Google/Service.php";
$client_id = '1045785081711- 95hsip42q972lgif5ib744ctrejj9kh3.apps.googleusercontent.com';
$Email_address = 'calendarosbooking#calendarproject1189.iam.gserviceaccount.com';
$key_file_location = '/var/www/www.junkpolice.com/html/components/com_osservicesbooking/Calendar Project-e0174e023f1a.p12';
$gCalenderID = 'junkpolice2060#gmail.com';
try {
$client = new Google_Client();
$client->setApplicationName("CalendarProject");
$client->setClientId($client_id);
$client->setAssertionCredentials(
new Google_Auth_AssertionCredentials(
$Email_address,
array("https://www.googleapis.com/auth/calendar"),
file_get_contents($key_file_location),
'notasecret','http://oauth.net/grant_type/jwt/1.0/bearer',false,false
)
);
}
catch (RuntimeException $e) {
return 'Problem authenticating Google Calendar:'.$e->getMessage();
}
$service = new Google_Service_Calendar($client);
$newEvent = new Google_Service_Calendar_Event();
$newEvent->setSummary($Service);
$newEvent->setLocation($Address);
$newEvent->setDescription($Notes);
$event_start = new Google_Service_Calendar_EventDateTime();
$event_start->setDateTime($startTime);
$newEvent->setStart($event_start);
$event_end = new Google_Service_Calendar_EventDateTime();
$event_end->setDateTime($EndTime);
$newEvent->setEnd($event_end);
$createdEvent = null;
try {
$createdEvent = $service->events- >insert("junkpolice2060#gmail.com", $newEvent);
$createdEvent_id= $createdEvent->getId();
$db->setQuery("Update #__app_sch_order_items set gcalendar_event_id = '$createdEvent_id' where id = '$order_item_id'");
$db->query();
} catch (Google_ServiceException $e) {
//logIt("svgcal_v3,".$e->getMessage());
echo $e->getMessage();
exit;
}
Am using Google Api v3 for adding and modifying google events .
most of the thing works fine except am unable to get the changes made in google events
am using the below code :
require_once 'google-api-php-client/src/Google/Client.php';
require_once 'google-api-php-client/src/Google/Service/Analytics.php';
require_once 'google-api-php-client/src/Google/Service/Calendar.php';
$client_id = 'my-client_id'; // works without this
$Email_address = 'my-Email_address';
$key_file_location = 'my-key_file_location.p12';
$client = new Google_Client();
$client->setApplicationName("app-name"); // works without random names
$key = file_get_contents($key_file_location);
$scopes = array('https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/calendar.readonly');
$cred = new Google_Auth_AssertionCredentials($Email_address, $scopes , $key);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired())
{
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$service = new Google_Service_Calendar($client);
$cals = $service->calendarList->listCalendarList();
$event = new Google_Service_Calendar_Event();
$event->setSummary('04/03 appointment -1');
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2015-03-27T11:00:00.000-00:00');
$start->setTimeZone('Asia/Calcutta');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2015-03-27T11:25:00.000-00:00');
$end->setTimeZone('Asia/Calcutta');
$event->setEnd($end);
$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setEmail('myemail#gmail.com');
$attendees = array($attendee1);
$event->attendees = $attendees;
$recurringEvent = $service->events->insert('randome-id#group.calendar.google.com', $event);
echo $dynamicId = $recurringEvent->getId();
$events = $service->events->listEvents('randome-id#group.calendar.google.com');
echo "<br>start - into web hoot <br>";
$service = new Google_Service_Calendar($client);
$channel = new Google_Service_Calendar_Channel($client);
$channel->setId($dynamicId);
$channel->setType('web_hook');
echo '<br>';
echo $url = 'https://mydomain.com.return.php';
$channel->setAddress($url);
$timetoExpire = time()+3600000;
$optParams = array('ttl' => $timetoExpire);
$channel->setParams($optParams);
try
{
echo "<br>";
$watchEvent = $service->events->watch('randome-id#group.calendar.google.com', $channel);
print_r($watchEvent);
echo "<br>hook applied<br>";
}
catch(Exception $e)
{
echo $e;
echo "web hook not working<br />.";
}
echo "<br>end";
exit;
return response comes properly in https://mydomain.com.return.php but still am not sure how to read the response
and am unsure
what is
[resourceId] =7U94nhlXTXH_tK13-5JIrZTBWU0
[resourceUri] =https://www.googleapis.com/calendar/v3/calendars/randome-id#group.calendar.google.com/events?alt=json
please help me to figure out how to get the modified event date in goggle calender Thanks in advance
I'm using Google API PHP client to add events to my calendar. ID is returned successfully after the insert & When I fetch that event using the ID it displays summary & everything correctly & for some reason , the event is showing up in my calendar
I'm using Service Account Credentials. Is there something to be enabled to get this working?
Here's my Code
session_start();
require dirname(__FILE__).'/google-api-php-client-master/autoload.php';
$client_id = '<client ID>';
$service_account_name = '<Service Account Name>';
$key_file_location = dirname(__FILE__).'/API-Project-96c2a9122085.p12';
if (!strlen($service_account_name) || !strlen($key_file_location)) {
echo missingServiceAccountDetailsWarning();
}
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$service = new Google_Service_Calendar($client);
if (isset($_SESSION['service_token'])) {
$client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials($service_account_name,array('https://www.googleapis.com/auth/calendar'),$key);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
$event = new Google_Service_Calendar_Event();
$event->setSummary("WHY IS THIS NOT WOKING?");
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime(date('Y-m-d\TH:i:s',strtotime('2014-12-24 10:00:00')));
$start->setTimeZone('Australia/Melbourne');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime(date('Y-m-d\TH:i:s',strtotime('2014-12-26 12:00:00')));
$end->setTimeZone('Australia/Melbourne');
$event->setEnd($end);
$newEvent = $service->events->insert('primary', $event);
$newEvent->getId();
$event = $service->events->get('primary', $newEvent->getId());
echo '<pre>'; print_r($event); echo '</pre>';
With the new Google_Client API, you need to share your calendar to yourself.
$scope = new Google_Service_Calendar_AclRuleScope();
$scope->setType('user');
$scope->setValue( 'Your Email Goes Here' );
$rule = new Google_Service_Calendar_AclRule();
$rule->setRole( 'owner' );
$rule->setScope( $scope );
$result = $service->acl->insert('primary', $rule);
Let me know if you still have issue.
I seem to have bumped into a google calendar api problem. and I can't really see where I have made the error so an extra set of eyes would be greatly appreciated. The problem in short is that the part of the code that should output the events.. doesn't (the "result" part of the code.
require_once "./google-api-php-client/src/Google/Client.php";
require_once "./google-api-php-client/src/Google/Service/Calendar.php";
// Service Account info
$client_id = "XXXXXX-XXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com";
$app_name = "Some-name";
$service_account_name = 'XXXXX-XXXXXXXXXXXXXXXXXXXXXXX#developer.gserviceaccount.com';
$key_file_location = 'google-api-php-client/Some-name-xxxxxxxxxx.p12';
$cal_id = "primary";
// Service Account info
$client_id = $client_id;
$service_account_name = $service_account_name;
$key_file_location = $key_file_location;
// Calendar id
$calName = $cal_id;
$client = new Google_Client();
$client -> setApplicationName($app_name);
$service = new Google_Service_Calendar($client);
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array('https://www.googleapis.com/auth/calendar.readonly'),
$key
);
$client->setAssertionCredentials($cred);
$cals = $service->calendarList->listCalendarList();
print_r($cals);
echo "<br>events<br>";
$events = $service->events->listEvents($calName);
var_dump($events);
echo "<br>result:<br>";
// the following is what returns nothing, works fine until this point.
$i = 0;
foreach ( $events->getItems() as $event ) {
echo 'i:'.$i.' '.$event->getSummary();
$i++;
}
echo 'end';
Change $events to the below line:
$events = $service->events->listEvents('primary', $params);
Instead of primary, add $calName and also send $params in listEvents method.
Hope this works.
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.