Comments (4)
The $response->getBody()
should be enough, however when calling it for the first time, you've to rewind
it if you're planning to access content for the second time.
For example, I have called $response->getBody()
, then when calling getContents();
, the result was always empty. Doing $response->rewind();
in this case helped.
Related: Guzzlehttp - How get the body of a response from Guzzle 6?
$contents = (string) $response->getBody();
vs
$contents = $response->getBody()->getContents();
The difference between the two approaches is that
getContents
returns the remaining contents, so that a second call returns nothing unless you seek the position of the stream withrewind
orseek
.
from psr7.
If you need to seek and store the entire stream in memory then cast the stream to a string using (string)
On Sep 14, 2015, at 8:49 AM, Veaceslav Medvedev [email protected] wrote:
Hello, I hit one problem when use instance of GuzzleHttp\Psr7\Stream.
echo $response->getBody()->getContents();
method returns me empty string, although the logger show me correct response.The problem was that the logger use (string) $response->getBody() and we seek to the end of stream.
Should we seek back before returns content?
public function getContents()
{
$this->rewind();
$contents = stream_get_contents($this->stream);if ($contents === false) { throw new \RuntimeException('Unable to read stream contents'); } return $contents;
}
Seems __toString() can call getContent().β
Reply to this email directly or view it on GitHub.
from psr7.
Well, I think getContents
meaning all body contents, not a part. To read body partial we have read($length)
. Or I''m wrong?
from psr7.
See https://github.com/php-fig/http-message/blob/master/src/StreamInterface.php#L143
from psr7.
Related Issues (20)
- PHP 8.2 Deprecated Messages HOT 3
- GuzzleHttp\Psr7\Message::bodySummary() incorrect cut off characters in unicode encoding HOT 1
- Missing typehints HOT 1
- GuzzleHttp\Psr7\Uri Incorrectly filled port, when converted to a string HOT 2
- Message::toString throw error when headers is list array HOT 1
- Content-Type assumption not done if the file don't have extension HOT 4
- $response->getBody()->getContents() returns empty string HOT 4
- "HTTP/2 200 is not valid header value" on 2.5.0 HOT 11
- AAC MimeType has a Typo HOT 5
- Remove ralouphie/getallheaders dependency ? HOT 5
- Exception in \GuzzleHttp\Psr7\Message::toString with poor (numeric) header HOT 3
- `Uri::__toString()` can yield malformed URIs HOT 1
- Header::parse loosing cookies with same name
- Declaration Error in GuzzleHttp\Psr7\Uri because of missing type hint for arguments. HOT 5
- summarize the multibytes(utf8) payload stream maybe cause the content unreadable
- Cross Origin - Allow optional protocol upgrade HOT 4
- StreamWrapper::stream_stat() should not always return an array
- Parsing Link header doesnβt handle URLs with commas HOT 1
- Header validation (InvalidArgumentException : ":<header>" is not valid header name) HOT 6
- Change \GuzzleHttp\Psr7\Utils::modifyRequest to keep initial Request object with modifications instead of creating new Request
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from psr7.