1

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?

Peter
  • 3,144
  • 11
  • 37
  • 56

2 Answers2

2

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.

David Myers
  • 799
  • 1
  • 9
  • 18
1

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

steven
  • 4,868
  • 2
  • 28
  • 58
  • Yes, it returns the following: `Notice: Undefined property: Github\HttpClient\Message\Response::$statusCode` – Peter Jun 19 '13 at 05:26
  • hmm its shown as private... same notice with remainingCalls too? – steven Jun 19 '13 at 05:26
  • That *appears* to be working. Every test I've run that value has always been empty but I'm not seeing any notices. – Peter Jun 19 '13 at 05:28
  • ok, you got a 200 so there should be no remaining calls. But what about headers and content (forget it, they are private, too). Please inspect the sourcecode of the Response class and try to find a method which gives you what you want. You should be able to call every public method of Github\HttpClient\Message\Response – steven Jun 19 '13 at 05:31
  • headers and content have content. Headers is a key=>value array and content is a json encoded string. It is odd that even though the method does show as private that you can still loop through it manually. – Peter Jun 19 '13 at 05:33
  • thats not odd, maybe there is a getter or a setter for it in Response like shown here: http://stackoverflow.com/questions/4478661/getter-and-setter . So if you have the json or array data you can do what you want now. – steven Jun 19 '13 at 05:38