I'm trying to insert multiple events to the google calendar with PHP. When I try to insert one event with this code below, it works, but when I want to insert multiple events on google calendar API server, to save more data, i get some error.
This is the code that I have:
I have an JSON string, this string was converted to a php array.
$calendar =
json_decode('
{
"summary": "Calendar name",
"description": "Calendar description",
"items":
[
{
"summary": "event1",
"colorId": "10",
"start": {
"dateTime": "2018-05-10T12:30:00+02:00"
},
"end": {
"dateTime": "2018-05-10T13:30:00+02:00"
},
"recurrence": [
"RRULE:FREQ=WEEKLY;COUNT=13;BYDAY=TH"
]
},
{
"summary": "event2",
"start": {
"dateTime": "2018-05-17T12:30:00+02:00"
},
"end": {
"dateTime": "2018-05-17T13:30:00+02:00"
}
},
{
"summary": "event3",
"description": "event3 description",
"colorId": "2",
"start": {
"date": "2018-05-10"
},
"end": {
"date": "2018-05-11"
},
"transparency": "transparent"
}
]
}
', true);
With this functions, I can add only one event to google calendar. When I try to insert more events I get errors...
function CreateCalendarEvents($calendar_id, $access_token, $events_json) {
$url_events = 'https://www.googleapis.com/calendar/v3/calendars/'.$calendar_id.'/events';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_events);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token, 'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($events_json));
$data = json_decode(curl_exec($ch), true);
$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
if($http_code != 200){
throw new Exception('Error : Failed to create events');
}
return $data['id'];}
I call function with this line of code:
CreateCalendarEvents('primary', $_SESSION['access_token'], $calendar);