代码之家  ›  专栏  ›  技术社区  ›  Ed Dunn

PHP访问对象的私有成员

  •  0
  • Ed Dunn  · 技术社区  · 7 年前

    我以前见过这个问题,但我想对我的情况再做一点澄清。我正在尝试使用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 回复  |  直到 7 年前
        1
  •  0
  •   Barmar    7 年前

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

    print_r(get_object_vars($events[1])['modelData']['creator']->email);
    
        2
  •  0
  •   Ed Dunn    7 年前

    多亏了你的帮助,它为我指明了正确的方向。我找到了以下解决方案

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

    这至少为我提供了所需的信息,这些信息将用于其他功能。