[英]PHP Guzzle client for HTTP keep-alive
I am trying to connect to an API that keeps the connection alive and sends the chunked data. 我正在尝试连接到一个API,使连接保持活动状态并发送分块数据。
I have a solution that uses fsockopen which writes and reads the stream and uses while(!$stream->eof())
. 我有一个使用fsockopen的解决方案,它写入和读取流并使用while(!$stream->eof())
。
I am wondering if I can use Guzzle to acheive the same result? 我想知道我是否可以使用Guzzle来获得相同的结果?
I saw the Async method but can some show me how to wait for stream? 我看到了Async方法,但有些人可以告诉我如何等待流吗?
Guzzle 6+ supports PRS-7 , which defines a response body as a stream. Guzzle 6+支持PRS-7 ,它将响应主体定义为流。 You can request data synchronously or asynchronously, it doesn't matter. 您可以同步或异步请求数据,这无关紧要。
Also it's important to set stream
option for your request: 为您的请求设置stream
选项也很重要:
$client = new Client(/* ... */);
$response = $client->get('http://some.url/', ['stream' => true]);
$bodyStream = $response->getBody();
while (!$bodyStream->eof()) {
echo $bodyStream->read(1024);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.