Search code examples
phpcurlgoogle-calendar-apigoogle-api-php-client

Invalid recurrence rule?


Here is the payload that I'm passing to Google Calendar API using cURL:

["start"]=>
  string(25) "2019-07-01T00:00:00+08:00"
  ["end"]=>
  string(25) "2019-07-16T00:00:00+08:00"
  ["title"]=>
  string(20) "Google Sync Weekly 4"
  ["description"]=>
  string(20) "Google Sync Weekly 4"
  ["recurrence"]=>
  string(44) "RRULE=WEEKLY;UNTIL=2019-07-16T00:00:00+08:00"

And in my new_event.php, where I process this payload:

$event = new Google_Service_Calendar_Event(array(
            'summary' => $event['title'],
            'location' => !empty($event['location']) ? $event['location'] : '',
            'description' => $event['description'],
            'start' => array(
              'dateTime' => $event['start'],
              'timeZone' => 'Asia/Taipei',
            ),
            'end' => array(
              'dateTime' => $event['end'],
              'timeZone' => 'Asia/Taipei',
            ),
            'recurrence' => !empty($event['recurrence']) ? [$event['recurrence']] : [],
            'reminders' => array(
              'useDefault' => FALSE,
              'overrides' => array(
                array('method' => 'email', 'minutes' => 24 * 60),
                array('method' => 'popup', 'minutes' => 10),
              ),
            ),
          ));

Here is the value:

["recurrence"]=>
  array(1) {
    [0]=>
    string(44) "RRULE=WEEKLY;UNTIL=2019-07-16T00:00:00+08:00"
  }

I can't figure out what I got wrong here. Any hints?


Solution

  • The following works as expected:

    $event = [
      'start' => "2019-07-01T00:00:00+08:00",
      'end' => "2019-07-16T00:00:00+08:00",
      'title' => "Google Sync Weekly 4",
      'description' => "Google Sync Weekly 4",
      'recurrence' => "RRULE:FREQ=WEEKLY;UNTIL=20190716T000000Z",
    ];
    
    $event = new Google_Service_Calendar_Event(array(
      'summary' => $event['title'],
      'location' => !empty($event['location']) ? $event['location'] : '',
      'description' => $event['description'],
      'start' => array(
        'dateTime' => $event['start'],
        'timeZone' => 'Asia/Taipei',
      ),
      'end' => array(
        'dateTime' => $event['end'],
        'timeZone' => 'Asia/Taipei',
      ),
      'recurrence' => !empty($event['recurrence']) ? [$event['recurrence']] : [],
      'reminders' => array(
        'useDefault' => FALSE,
        'overrides' => array(
          array('method' => 'email', 'minutes' => 24 * 60),
          array('method' => 'popup', 'minutes' => 10),
        ),
      ),
    ));
    
    $service = new Google_Service_Calendar($client);
    $service->events->insert('primary', $event);
    

    The issue is, as the error message suggests, your recurrence rule is formatted incorrectly. Rules should be prefixed with RRULE: and must follow the rules laid out in RFC 5545. Pay close attention to the sections regarding timestamps and timezones. A basic example of a properly formatted rule may be found in the "Creating recurring events" code snippet in the API documentation site.

    You could also create a recurrence from the Calendar UI and fetch it via the API for other more tailored examples.