简体   繁体   中英

How to access object elements in an array

I'm working with the Github PHP Library . There is a specific call that you can make that uses the Buzz HTTP Client as shown by the following:

$client->getHttpClient()->get('repos/:user/:repo/events');

The problem is the return of that request is something that I'm unsure how to access the elements:

Github\HttpClient\Message\Response Object
(
    [remainingCalls] => 
    [protocolVersion:Buzz\Message\Response:private] => 0
    [statusCode:Buzz\Message\Response:private] => 200
    [reasonPhrase:Buzz\Message\Response:private] => OK
    [headers:Buzz\Message\AbstractMessage:private] => 

    [content:Buzz\Message\AbstractMessage:private] => 
)

Now I can loop through the object doing something like the following:

foreach( $events as $item ) {
   print_r( $item );
}

But I really only care about the content inside of headers and content .

Is anyone aware of how I can directly access those elements directly without the need to loop?

If you look at the source for that object class you'll see that there's a getContent() function that will retrieve the value of the content field. You will also notice that this class extends the Response class from the Buzz Client. The Buzz Response class in turn is an extension of the AbstractMessage class which has a getHeaders() function as well as a getContent() function.

So essentially you can access those two variables using the getContent() and getHeaders() functions provided through inheritance.

Have you tried this?

$res = $client->getHttpClient()->get('repos/:user/:repo/events');
echo $res->remainingCalls;  // non private var

or you should be able to call every public method of the Response Class

$res->getStatusCode();
$res->getProtocolVersion();

Inspect the possible methods here:

https://github.com/kriswallsmith/Buzz/blob/master/lib/Buzz/Message/Response.php

https://github.com/kriswallsmith/Buzz/blob/master/lib/Buzz/Message/AbstractMessage.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM