4

I am trying to connect to an API that keeps the connection alive and sends the chunked data.

I have a solution that uses fsockopen which writes and reads the stream and uses while(!$stream->eof()).

I am wondering if I can use Guzzle to acheive the same result?

I saw the Async method but can some show me how to wait for stream?

1 Answer 1

2

Guzzle 6+ supports PRS-7, which defines a response body as a stream. You can request data synchronously or asynchronously, it doesn't matter.

Also it's important to set stream option for your request:

$client = new Client(/* ... */);

$response = $client->get('http://some.url/', ['stream' => true]);
$bodyStream = $response->getBody();

while (!$bodyStream->eof()) {
    echo $bodyStream->read(1024);
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.