【问题标题】:PHP curl write response final callbackPHP curl 写响应最终回调
【发布时间】:2017-05-06 14:00:27
【问题描述】:

我正在尝试在 PHP 中开发一个反向代理服务器,并且正在使用 CURL 来发出 http 请求。

我正在使用 CURLOPT_WRITEFUNCTION 来配置我的写回调函数。

`

function curlReadCallback(&$param, $body)
     if ($this->_buffered) {
                        $this->_buffer .= $body;}
// process($this_buffer) // On final call, i want to call this 
}

`

在这个回调函数中,我创建了到目前为止累积的响应的缓冲区,一旦给出了所有响应,我想立即处理最终响应。问题是,我不知道如何知道这是最后一个回调,现在我可以继续处理完整的响应。

任何帮助将不胜感激!

【问题讨论】:

    标签: php curl reverse-proxy


    【解决方案1】:

    看起来您的代码只需要计算它发出的请求数并与收到的响应进行比较。

    给定:

    $this->requestsSent = 10;  // set to however many requests you send
    $this->responsesReceived = 0;
    

    你可以这样使用:

    function curlReadCallback(&$param, $body)
      $this->responsesReceived++;
      if ($this->_buffered) {
        $this->_buffer .= $body;
      }
      if ($this->responsesReceived == $this->requestsSent) {
        process($this->_buffer);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-16
      • 2013-09-21
      • 2021-10-16
      • 1970-01-01
      • 2014-10-31
      • 1970-01-01
      相关资源
      最近更新 更多