Two Ways to Get the Body
After getting a Response from GuzzleHttp PSR-7, there are two ways to retrieve the content:
| |
On the first call, both return the same result. But calling them twice reveals a difference.
The Difference
| |
Why
Looking at the source code makes it clear. __toString calls seek(0) to reset the stream pointer, so casting to string always reads the full content:
| |
getContents() does not reset the pointer – it reads from the current position forward. After the first read, the pointer is at the end, so the second read returns nothing.
If you need to use getContents() and read multiple times, call $response->getBody()->rewind() first.
