Mac Taylor
Mac Taylor

Reputation: 5148

Accessing private property in PHP Guzzle Response object

I tried to access to statuscode and reasonphrase but the bellow code returns erros like

die(var_dump($registrant->response->reasonPhrase));

error:

"message": "Undefined property: MacsiDigital\Zoom\Support\Response::$reasonPhrase",

code: ( RESULT OF var_dump($registrant->getResponse()); )

 object(MacsiDigital\Zoom\Support\Response)#874 (1) {
  ["response":protected]=>
  object(GuzzleHttp\Psr7\Response)#885 (6) {
    ["reasonPhrase":"GuzzleHttp\Psr7\Response":private]=>
    string(7) "Created"
    ["statusCode":"GuzzleHttp\Psr7\Response":private]=>
    int(201)
    ["headers":"GuzzleHttp\Psr7\Response":private]=>
    array(14) {
      ["Date"]=>
      array(1) {
        [0]=>
        string(29) "Sun, 16 Feb 2020 05:56:11 GMT"
      }
      ["Content-Type"]=>
      array(1) {
        [0]=>
        string(30) "application/json;charset=UTF-8"
      }
      ["Content-Length"]=>
      array(1) {
        [0]=>
        string(3) "249"
      }
      ["Connection"]=>
      array(1) {
        [0]=>
        string(10) "keep-alive"
      }
      ["Server"]=>
      array(1) {
        [0]=>
        string(4) "ZOOM"
      }
      ["x-zm-trackingid"]=>
      array(1) {
        [0]=>
        string(36) ""
      }
      ["X-Content-Type-Options"]=>
      array(1) {
        [0]=>
        string(7) "nosniff"
      }
      ["Cache-Control"]=>
      array(1) {
        [0]=>
        string(49) "no-cache, no-store, must-revalidate, no-transform"
      }
      ["Pragma"]=>
      array(1) {
        [0]=>
        string(8) "no-cache"
      }
      ["Expires"]=>
      array(1) {
        [0]=>
        string(29) "Thu, 01 Jan 1970 00:00:00 GMT"
      }
      ["Set-Cookie"]=>
      array(1) {
        [0]=>
        string(63) "cred=; Path=/; Secure; HttpOnly"
      }
      ["Strict-Transport-Security"]=>
      array(1) {
        [0]=>
        string(16) "max-age=31536000"
      }
      ["X-XSS-Protection"]=>
      array(1) {
        [0]=>
        string(13) "1; mode=block"
      }
      ["Referrer-Policy"]=>
      array(1) {
        [0]=>
        string(31) "strict-origin-when-cross-origin"
      }
    }
    ["headerNames":"GuzzleHttp\Psr7\Response":private]=>
    array(14) {
      ["date"]=>
      string(4) "Date"
      ["content-type"]=>
      string(12) "Content-Type"
      ["content-length"]=>
      string(14) "Content-Length"
      ["connection"]=>
      string(10) "Connection"
      ["server"]=>
      string(6) "Server"
      ["x-zm-trackingid"]=>
      string(15) "x-zm-trackingid"
      ["x-content-type-options"]=>
      string(22) "X-Content-Type-Options"
      ["cache-control"]=>
      string(13) "Cache-Control"
      ["pragma"]=>
      string(6) "Pragma"
      ["expires"]=>
      string(7) "Expires"
      ["set-cookie"]=>
      string(10) "Set-Cookie"
      ["strict-transport-security"]=>
      string(25) "Strict-Transport-Security"
      ["x-xss-protection"]=>
      string(16) "X-XSS-Protection"
      ["referrer-policy"]=>
      string(15) "Referrer-Policy"
    }
    ["protocol":"GuzzleHttp\Psr7\Response":private]=>
    string(3) "1.1"
    ["stream":"GuzzleHttp\Psr7\Response":private]=>
    object(GuzzleHttp\Psr7\Stream)#883 (7) {
      ["stream":"GuzzleHttp\Psr7\Stream":private]=>
      resource(650) of type (stream)
      ["size":"GuzzleHttp\Psr7\Stream":private]=>
      NULL
      ["seekable":"GuzzleHttp\Psr7\Stream":private]=>
      bool(true)
      ["readable":"GuzzleHttp\Psr7\Stream":private]=>
      bool(true)
      ["writable":"GuzzleHttp\Psr7\Stream":private]=>
      bool(true)
      ["uri":"GuzzleHttp\Psr7\Stream":private]=>
      string(10) "php://temp"
      ["customMetadata":"GuzzleHttp\Psr7\Stream":private]=>
      array(0) {
      }
    }
  }
}

PS. : when I trie to access like this it returns Null

$registrant->reasonPhrase

Upvotes: 0

Views: 960

Answers (2)

unclexo
unclexo

Reputation: 3941

Well, In PHP, you can not access private property outside the class. You can access protected property in the subclasses of a class. But if they are needed to access by clients/you (meaning who is using the object/code) developers of that kind of classes use getters and setters method to access them. So you can use them as the following if you need them.

$registrant->getResponse()->getResponse()->getReasonPhrase();

Actually this method comes from this Guzzle's class GuzzleHttp\Psr7\Response. If you want you can find it here

Upvotes: 2

Foued MOUSSI
Foued MOUSSI

Reputation: 4813

To inspect the private properties of your MacsiDigital\Zoom\Support\Response Obj, you have no choice but using setter/getter methods.

Try

dump($registrant->getResponse());

Then

dump(($registrant->getResponse())->getReasonPhrase());

Upvotes: 0

Related Questions