how to create an event on google calendar using php

后端 未结 3 701
温柔的废话
温柔的废话 2021-02-11 05:44

am trying to sync. events from my website to google *calendar* after

user give me a permission to write on calendar .

相关标签:
3条回答
  • 2021-02-11 06:12

    The way the documentation describes creating events worked nicely for me.

    According to the documentation:

    $event = new Google_Service_Calendar_Event(array(
      'summary' => 'Google I/O 2015',
      'location' => '800 Howard St., San Francisco, CA 94103',
      'description' => 'A chance to hear more about Google\'s developer products.',
      'start' => array(
        'dateTime' => '2015-05-28T09:00:00-07:00',
        'timeZone' => 'America/Los_Angeles',
      ),
      'end' => array(
        'dateTime' => '2015-05-28T17:00:00-07:00',
        'timeZone' => 'America/Los_Angeles',
      ),
      'recurrence' => array(
        'RRULE:FREQ=DAILY;COUNT=2'
      ),
      'attendees' => array(
        array('email' => 'lpage@example.com'),
        array('email' => 'sbrin@example.com'),
      ),
      'reminders' => array(
        'useDefault' => FALSE,
        'overrides' => array(
          array('method' => 'email', 'minutes' => 24 * 60),
          array('method' => 'popup', 'minutes' => 10),
        ),
      ),
    ));
    
    $calendarId = 'primary';
    $event = $service->events->insert($calendarId, $event);
    

    Assuming you already know how to retrieve an access token, a more complete implementation could be the following:

    require __DIR__ . '/vendor/autoload.php';
    
    /* Retrieve access token */
    
    $client = new Google_Client();
    $client->setApplicationName('Google Calendar API PHP Create Event');
    $client->setScopes(Google_Service_Calendar::CALENDAR);
    $client->setAuthConfig('credentials.json');
    $client->setAccessType('offline');
    $client->setAccessToken($accessToken);
    
    $service = new Google_Service_Calendar($client);
    
    $event = new Google_Service_Calendar_Event(array(
      'summary' => 'Google I/O 2015',
      'location' => '800 Howard St., San Francisco, CA 94103',
      'description' => 'A chance to hear more about Google\'s developer products.',
      'start' => array(
        'dateTime' => '2015-05-28T09:00:00-07:00',
        'timeZone' => 'America/Los_Angeles',
      ),
      'end' => array(
        'dateTime' => '2015-05-28T17:00:00-07:00',
        'timeZone' => 'America/Los_Angeles',
      ),
      'recurrence' => array(
        'RRULE:FREQ=DAILY;COUNT=2'
      ),
      'attendees' => array(
        array('email' => 'lpage@example.com'),
        array('email' => 'sbrin@example.com'),
      ),
      'reminders' => array(
        'useDefault' => FALSE,
        'overrides' => array(
          array('method' => 'email', 'minutes' => 24 * 60),
          array('method' => 'popup', 'minutes' => 10),
        ),
      ),
    ));
    
    $calendarId = 'primary';
    $event = $service->events->insert($calendarId, $event);
    printf('Event created: %s\n', $event->htmlLink);
    
    0 讨论(0)
  • 2021-02-11 06:15

    Have you tried to read the reference?

    Using quickAdd (which creates an event based on a simple string)

    https://developers.google.com/google-apps/calendar/v3/reference/events/quickAdd

    require_once 'google-api-php-client/src/Google_Client.php';
    require_once 'google-api-php-client/src/contrib/Google_CalendarService.php';
    
    $createdEvent = $service->events->quickAdd(
        'primary',
        'Appointment at Somewhere on June 3rd 10am-10:25am');
    
    echo $createdEvent->getId();
    

    Or using insert

    https://developers.google.com/google-apps/calendar/v3/reference/events/insert

    require_once 'google-api-php-client/src/Google_Client.php';
    require_once 'google-api-php-client/src/contrib/Google_CalendarService.php';
    
    $event = new Google_Event();
    $event->setSummary('Appointment');
    $event->setLocation('Somewhere');
    $start = new EventDateTime();
    $start->setDateTime('2011-06-03T10:00:00.000-07:00');
    $event->setStart($start);
    $end = new EventDateTime();
    $end->setDateTime('2011-06-03T10:25:00.000-07:00');
    $event->setEnd($end);
    $attendee1 = new EventAttendee();
    $attendee1->setEmail('attendeeEmail');
    // ...
    $attendees = array($attendee1,
                       // ...
                      );
    $event->attendees = $attendees;
    $createdEvent = $service->events->insert('primary', $event);
    
    echo $createdEvent->getId();
    
    0 讨论(0)
  • 2021-02-11 06:18

    The reference seems out of date - the class names are incomplete (creating google calendar event)

    Here's the updated snippet:

    require_once 'google-api-php-client/src/Google_Client.php';
    require_once 'google-api-php-client/src/contrib/Google_CalendarService.php';
    
    $event = new Google_Event();
    $event->setSummary('Appointment');
    $event->setLocation('Somewhere');
    $start = new Google_EventDateTime();
    $start->setDateTime('2011-06-03T10:00:00.000-07:00');
    $event->setStart($start);
    $end = new Google_EventDateTime();
    $end->setDateTime('2011-06-03T10:25:00.000-07:00');
    $event->setEnd($end);
    $attendee1 = new Google_EventAttendee();
    $attendee1->setEmail('attendeeEmail');
    // ...
    $attendees = array($attendee1,
                       // ...
                      );
    $event->attendees = $attendees;
    
    $createdEvent = $cal->events->insert('primary', $event);
    echo $createdEvent->id;
    
    0 讨论(0)
提交回复
热议问题