0

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);
Ok Ram
  • 41
  • 5
  • as per the API docs you have to make one API call per event you want to add. https://developers.google.com/calendar/v3/reference/events/insert . It is not designed for bulk inserts, but if you have a small number of events to add, you can just make repeated calls to the same endpoint and send different data each time. BTW you would probably find it much easier and more reliable to use the PHP Google API libraries than to make your own cURL requests. See https://developers.google.com/api-client-library/php/ – ADyson May 14 '18 at 09:40
  • update: it turns out you can also use the "batch" method as well to perform multiple operations in one request: https://developers.google.com/calendar/batch – ADyson May 24 '18 at 11:52

0 Answers0