PHP访问对象的私有成员

时间:2018-07-25 20:57:59

标签: php google-api-php-client

我以前曾经看过这个问题,但是我想对我的情况进行更多的说明。我想做的是使用php从Google日历api访问事件数据。数据返回到一个对象中,但是我需要解析和使用的信息受到保护。我不确定该如何解决。线程

How to get protected property value of object in PHP

与我目前的情况基本相同,似乎该人找到了一个解决方案,但没有发布任何代码或任何形式的更新来反映所做的事情。希望有人可以进一步澄清。这是我当前正在使用的代码,非常感谢见识

<?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);

print_r语句显示以下内容(我将其截断)

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
            (
            )

    )

就像我发布的链接中的帖子一样,我也希望访问每个事件的开始和结束日期。

2 个答案:

答案 0 :(得分:0)

您可以使用get_object_vars(),它返回一个包含对象非静态属性的关联数组。

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

答案 1 :(得分:0)

感谢您的所有帮助,它为我指明了正确的方向。我找到了以下解决方案

    $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');
    }

这至少给了我所需的信息,这些信息将在其他功能中使用。

相关问题