【发布时间】:2019-03-26 09:07:11
【问题描述】:
使用 php 调用 curl 时,我可以在 CURLOPT_PROGRESSFUNCTION 上挂钩回调,并在请求过程中使用 curl_multi_getcontent($handle) 读取标头
$handle = curl_init()
curl_setopt(CURLOPT_NOPROGRESS, false)
curl_setopt(CURLOPT_RETURNTRANSFER, true)
curl_setopt(CURLOPT_PROGRESSFUNCTION, function($handle) {
$response = curl_multi_getcontent($handle);
// some logic here
})
curl_exec($handle)
如何使用 Guzzle 做到这一点?
问题是如果不将CURLOPT_RETURNTRANSFER 设置为true,我将无法使用curl_multi_getcontent($handle)。
但是当我将CURLOPT_RETURNTRANSFER 设置为guzzle' curl 配置时,我可以读取正在进行的标题功能$response = curl_multi_getcontent($handle); 但是响应流包含空内容。
$request->getResponse()->getBody()->getContents(); // always outputs ""
编辑: 我已经做了这个更改https://github.com/guzzle/guzzle/pull/2173,所以我可以使用进度设置访问处理中的回调:
'progress' => function($handle) {
$response = curl_multi_getcontent($handle);
// some logic here
})
只要CURLOPT_RETURNTRANSFER 是true,它就可以工作。但是正如我之前提到的,响应内容是""。
【问题讨论】: