23

When I use Postman to make an API call I receive a JSON object..which is what I expected.enter image description here

However When I make same call with Guzzle like so:

$client = new \GuzzleHttp\Client(['base_uri' => 'https://api.dev/']);

$response = $client->request('GET', 'search', [
    'verify' => false,
]);

var_dump($response->getBody()); //null

var_dump($response); //returns below

I get dump below from Guzzle

Response {#304 ▼
  -reasonPhrase: "OK"
  -statusCode: 200
  -headers: array:8 [▼
    "Server" => array:1 [▶]
    "Content-Type" => array:1 [▼
      0 => "application/json"
    ]
    "Transfer-Encoding" => array:1 [▶]
    "Connection" => array:1 [▶]
    "Cache-Control" => array:1 [▶]
    "Date" => array:1 [▶]
    "X-RateLimit-Limit" => array:1 [▶]
    "X-RateLimit-Remaining" => array:1 [▶]
  ]
  -headerNames: array:8 [▼
    "server" => "Server"
    "content-type" => "Content-Type"
    "transfer-encoding" => "Transfer-Encoding"
    "connection" => "Connection"
    "cache-control" => "Cache-Control"
    "date" => "Date"
    "x-ratelimit-limit" => "X-RateLimit-Limit"
    "x-ratelimit-remaining" => "X-RateLimit-Remaining"
  ]
  -protocol: "1.1"
  -stream: Stream {#302 ▼
    -stream: stream resource @15 ▼
      wrapper_type: "PHP"
      stream_type: "TEMP"
      mode: "w+b"
      unread_bytes: 0
      seekable: true
      uri: "php://temp"
      options: []
    }
    -size: null
    -seekable: true
    -readable: true
    -writable: true
    -uri: "php://temp"
    -customMetadata: []
  }
}
Emeka Mbah
  • 16,745
  • 10
  • 77
  • 96

3 Answers3

83

getBody() returns a stream. If you want to get all the content at once you can use getContents() method and decode json while at it (if you need to)

$payload = json_decode($response->getBody()->getContents());

Further reading - Guzzle Responses

peterm
  • 91,357
  • 15
  • 148
  • 157
  • 1
    best answer i found for object not found or null with guzzle, thanks –  May 11 '19 at 16:54
  • 3
    The answer can be simplified to `json_decode($response->getBody())`. `json_decode()` will cast the body to `string` which has the same effect as calling `getContents()`. – Maksim Ivanov Nov 29 '19 at 15:51
  • @MaksimIvanov No it does not cast body to `string`. – Emre Aydin Jul 03 '20 at 03:46
  • @EmreAydin https://www.php.net/manual/en/function.json-decode.php – Maksim Ivanov Jul 11 '20 at 13:42
  • @MaksimIvanov `json_decode` expects first argument to be string. `getBody()` does not return string. – Emre Aydin Jul 11 '20 at 23:37
  • @EmreAydin correct, it expects the first argument to be string. And if it's not string, it casts it to string. – Maksim Ivanov Jul 13 '20 at 09:17
  • @MaksimIvanov As I experimented lately, it didn't. That's why I ended up in this thread.. – Emre Aydin Jul 22 '20 at 21:22
  • @EmreAydin and I have working code that relies on this cast :) could be that our PHP versions are different. – Maksim Ivanov Jul 24 '20 at 09:03
  • In windows local environment, PHP 7.2 and PHP 7.3 both returns empty string with `$response->getBody()->getContents()`. Had to use `json_decode($response->getBody());`. – Philip Borbon Oct 28 '20 at 09:08
  • That actually solved my problems with the oauth/token post request that I had. Proving that GuzzleHttp and all auxiliary functions were fetching the data and there was no problem with them. I just couldn't see the presented stream. Exceptional Answer! – incalite Jul 09 '21 at 10:31
27

If $response->getBody()->getContents() doesn't work for you, try:

$response->getBody()->__toString();

As mentioned here, sometimes getContents's stream pointer is already at the end of stream and then it returns empty response, but __toString resets it by default.

Milan Markovic
  • 1,250
  • 13
  • 18
  • This one worked for me. Is there any way to convert this string to PHP array? – Murat Colyaran Dec 20 '21 at 13:52
  • You just made my day. That made me really mad until I have found your answer. This seems still not to be solved that, I simply do not understand why they are doing this with the stream pointer. Any reason and any way to reset it? – Blackbam Feb 10 '22 at 17:28
  • I must say thank you for pointing us to this tricky thing! You've saved my day too! – Vlad Moyseenko Mar 08 '22 at 23:05
0

Milan's solution worked for me but the response coming from API was malformed.

I had to use stripslashes to convert it to a PHP array:

$response = $response->getBody()->__toString();
$response =  stripslashes($response);
$data = json_decode($response, true);
Murat Colyaran
  • 2,075
  • 2
  • 8
  • 27