Skip to content
Advertisement

Google Calendar API PHP listEvents not retrieving all events

i am trying to retrieve ALL of the events using the Google_CalendarService. So far I am retrieving everything with recuring events, but there are some events that don’t show up.

Example: I have events on 22-okt, 23-okt and 24-okt. They are all normal events, but the only ones I am getting are 22 and 23. 24 is not showing up, but it does exist in my calendar!

[UPDATE] Found another problem! Each time I edit an existing event via my app, the event disappears, but does exist in the calendar!

Here is my code for fetching data:

API settings:

function calSettings(){

    $client = new Google_Client();

    $cal = new Google_CalendarService($client);

    $client->setAccessType('offline');

    if (isset($_GET['code'])) {

        $client->authenticate($_GET['code']);

        $_SESSION['token'] = $client->getAccessToken();

        header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);

    }


    if (isset($_SESSION['token'])) {


        $client->setAccessToken($_SESSION['token']);


        $sessionToken = json_decode($_SESSION['token']);


        $this->Cookie->write('token', $sessionToken->refresh_token, false, '1 month');
    }


    $cookie = $this->Cookie->read('token');

    if(!empty($cookie)){
        $client->refreshToken($this->Cookie->read('token'));
    }


    $serviceArr = array('client' => $client, 'service' => $cal);

    return $serviceArr;
}

Get events:

public function initCalendar($ownerCalendarId = null, $eventID = null){

    $serviceArr = $this->calSettings();
    $client = $serviceArr['client'];
    $cal = $serviceArr['service'];
    $wandaCalendarId = 'gmail@gmail.com';

    if ($client->getAccessToken()) {
        /************* Code entry *************/
        if($eventID != null){
            $events = $cal->events->get($ownerCalendarId, $eventID);
        }else{
            $ownerEvents = $cal->events->listEvents($ownerCalendarId);

            if($ownerCalendarId != $wandaCalendarId){
                $wandaEvents = $cal->events->listEvents($wandaCalendarId);
                $events = array('owner' => $ownerEvents, 'wanda' => $wandaEvents);
            }else{
                $events = array('owner' => $ownerEvents, 'wanda' => '');
            }
        }

        return $events;

    }else{
        /************* Not connected to google calendar code *************/
        $authUrl = $client->createAuthUrl();

        $returnArr = array('status' => 'false', 'message' => "<a class='login' href='$authUrl'>Connect Me!</a>");

        return $returnArr;
    }
}

So with this code, I get most of the events, why arent the other ones showing up? Does anyone had this problem before?

Advertisement

Answer

Ok, finally found out how to get ALL the events!

The funny thing is that you DO get all the events, but in batches of 250 events. If you have more than 250 events, you will get in your JSON response a page token. Use the page token to get the next batch of events!

About the editing part? I’ve noticed that when you edit an event, the event will be thrown at the bottom of the event list. So the events that you get are sorted by last edited.

To avoid data you don’t need or keeping it simple, you can filter the events using timeMin and timeMax. Depending on what you do, you can dynamicly set this to change the request query.

Here is my version of getting the events I want with page tokens:

$minCheck = date(DATE_ATOM, mktime(0, 0, 0, 1, 1, date("Y") -1));
$maxCheck = date(DATE_ATOM, mktime(0, 0, 0, 12, 31, date("Y") +1));

do{
if(isset($result['nextPageToken'])){
   $result= $cal->events->listEvents($calendarId, array('singleEvents' => 'true', 'pageToken' => $result['nextPageToken'], 'timeMin' => $minCheck, 'timeMax' => $maxCheck));
}else{
   $result= $cal->events->listEvents($calendarId, array('singleEvents' => 'true', 'timeMin' => $minCheck, 'timeMax' => $maxCheck));
}
array_push($totalResult, $result['items']);
}while(isset($result['nextPageToken']) && !empty($result['nextPageToken']));
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement