1

I have seen this question once before but I am looking for a little more clarification related to my situation. What I am trying to do is access event data from the google calendar api using php. The data is returned in an object but the information I need to parse and use is protected. I am not sure how to get around this. This thread

How to get protected property value of object in PHP

is essentially identical to my current situation and it seems like the person found a solution yet didn't post any code or any sort of update to reflect what was done. Hopefully some one can clarify it more. Here is the code I am currently using, insight would be greatly appreciated

<?php
require_once 'vendor/autoload.php';

$maxEvents = 100;
$minStartDate = date('c');
$maxEndDate = date('c',strtotime("+1 day"));

$calendarId = '<MY CALENDAR ID>';

putenv("GOOGLE_APPLICATION_CREDENTIALS=<MY CREDENTIALS>");
$scope = 'https://www.googleapis.com/auth/calendar';

$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes($scope);
$service = new Google_Service_Calendar($client);

$options = array(
    'maxResults'    => $maxEvents,
    'orderBy'       => 'startTime',
    'singleEvents'  => TRUE,
    'timeMin'       => $minStartDate,
    'timeMax'       => $maxEndDate,
);

$results = $service->events->listEvents($calendarId, $options);
$events = $results->getItems();

$num = count($events);
echo $num . '<br/>';
echo 'results<br><pre>';print_r($events); echo '</pre><br>';

echo $events[1]->summary;
print_r($events[1]->modelData['creator']->email);

The print_r statements show the following (I'm truncating it)

Array
(
    [0] => Google_Service_Calendar_Event Object
    (
        [collection_key:protected] => recurrence
        [anyoneCanAddSelf] => 
        [attachmentsType:protected] => Google_Service_Calendar_EventAttachment
        [attachmentsDataType:protected] => array
        [attendeesType:protected] => Google_Service_Calendar_EventAttendee
        [attendeesDataType:protected] => array
        [attendeesOmitted] => 
        [colorId] => 
        [conferenceDataType:protected] => Google_Service_Calendar_ConferenceData
        [conferenceDataDataType:protected] => 
        [created] => 2017-11-02T18:29:30.000Z
        [creatorType:protected] => Google_Service_Calendar_EventCreator
        [creatorDataType:protected] => 
        [description] => 
Original Schedule Date - 2018-12-12
        [endType:protected] => Google_Service_Calendar_EventDateTime
        [endDataType:protected] => 
        [endTimeUnspecified] => 
        [etag] => "3034871549678000"
        [extendedPropertiesType:protected] => Google_Service_Calendar_EventExtendedProperties
        [extendedPropertiesDataType:protected] => 
        [gadgetType:protected] => Google_Service_Calendar_EventGadget
        [gadgetDataType:protected] => 
        [guestsCanInviteOthers] => 
        [guestsCanModify] => 
        [guestsCanSeeOtherGuests] => 
        [hangoutLink] => 
        [htmlLink] => 
        [iCalUID] => 
        [id] => 
        [kind] => calendar#event
        [location] => 
        [locked] => 
        [organizerType:protected] => Google_Service_Calendar_EventOrganizer
        [organizerDataType:protected] => 
        [originalStartTimeType:protected] => Google_Service_Calendar_EventDateTime
        [originalStartTimeDataType:protected] => 
        [privateCopy] => 
        [recurrence] => 
        [recurringEventId] => 
        [remindersType:protected] => Google_Service_Calendar_EventReminders
        [remindersDataType:protected] => 
        [sequence] => 2
        [sourceType:protected] => Google_Service_Calendar_EventSource
        [sourceDataType:protected] => 
        [startType:protected] => Google_Service_Calendar_EventDateTime
        [startDataType:protected] => 
        [status] => confirmed
        [summary] => 
        [transparency] => 
        [updated] => 2018-01-31T21:56:14.839Z
        [visibility] => 
        [internal_gapi_mappings:protected] => Array
            (
            )

        [modelData:protected] => Array
            (
                [creator] => Array
                    (
                        [email] => 
                        [displayName] => 
                    )

                [organizer] => Array
                    (
                        [email] => 
                        [self] => 1
                    )

                [start] => Array
                    (
                        [date] => 2018-07-25
                    )

                [end] => Array
                    (
                        [date] => 2018-07-26
                    )

                [attendees] => Array
                    (
                        [0] => Array
                            (
                                [email] => 
                                [responseStatus] => needsAction
                            )

                    )

                [reminders] => Array
                    (
                        [useDefault] => 1
                    )

            )

        [processed:protected] => Array
            (
            )

    )

Like the post from the link I posted I too am looking to access the start and end dates of each event.

Ed Dunn
  • 962
  • 2
  • 8
  • 22
  • Possible duplicate of [Can I get the value of a private property with Reflection?](https://stackoverflow.com/questions/11604946/can-i-get-the-value-of-a-private-property-with-reflection) – Goodbye StackExchange Jul 25 '18 at 21:04
  • 1
    There should be a `getSomething` method to get what you want. – AbraCadaver Jul 25 '18 at 21:06
  • https://stackoverflow.com/questions/27064642/how-to-get-event-details-with-calendar-api-v3 – AbraCadaver Jul 25 '18 at 21:07
  • https://developers.google.com/resources/api-libraries/documentation/calendar/v3/php/latest/class-Google_Service_Calendar_Event.html there are some methods that will help you – Felippe Duarte Jul 25 '18 at 21:08
  • thanks for the replies, using the methods to retrieve the start date still throws and error, maybe because I am using the listEvents method because I need to grab dates within a range and will not have a specific event id to use. I'll keep looking. – Ed Dunn Jul 25 '18 at 21:18
  • You should loop through the array returned by `listEvents()` and call the methods on the elements. – Barmar Jul 25 '18 at 21:29
  • As mentioned in a comment in the question you linked to, you can use [`get_object_vars()`](http://php.net/manual/en/function.get-object-vars.php) – Barmar Jul 25 '18 at 21:32

2 Answers2

0

You can use get_object_vars(), which returns an associative array containing the non-static properties of the object.

print_r(get_object_vars($events[1])['modelData']['creator']->email);
Barmar
  • 596,455
  • 48
  • 393
  • 495
0

Thanks to all your help, it pointed me in the right direction. I found the following solution

    $events = $service->events->listEvents($calendarId, $options);

    foreach ($events->getItems() as $event) {
        echo "Summary:  ", $event->getSummary(), "<br/>";
        echo "Location: ", $event->getLocation(), "<br/>";
        echo "Start:    ", parse_date($event->getStart()), "<br/>";
        echo "End:      ", parse_date($event->getEnd()), "<br/>";
        echo '<br/>';
    }

    function parse_date($date){
        $val = $date->getDate();
        return (new DateTime($val))->format('d/m/Y');
    }

This at least gives me the information I need which will be used in other functions.

Ed Dunn
  • 962
  • 2
  • 8
  • 22