mztest

curl_multi_init 操作实例

1. Method#multiRequest 接收需要处理的信息

这里的参数是我们所需要的处理的URL地址及其它信息。

/**
 * @param array (\'key\' => array())
 * You can find \'key\' inside of @method getApiMap
 * array(\'someMethodKey\'=>array(\'url\' => \'\', 
 *         \'requestData\' => \'post data\', 
 *         \'otherData\' => \'some other data\',
 *         \'curlOptions\' => array())));
 */
    
public function multiRequest($array)
{
    foreach($array as $k=>&$v) {
        $v[\'url\'] = $this->getApiURL($k);
    }
    return $this->_handleMultiResponse($this->_sendMultiRequest($array));
}

2. Method#_sendMultiRequest()

/**
     * Send multi request at same time
     * @param array $data (
     * array(\'url\' => \'\', 
     *         \'requestData\' => \'post data\', 
     *         \'otherData\' => \'some other data\',
     *         \'curlOptions\' => array()));
     */
    private function _sendMultiRequest(array $data)
    {
        $this->_logMultiRequest($data);
        $conn = array();
        //create the multiple cURL handle
        $mh = curl_multi_init();
        $_options = array(
            CURLOPT_HEADER => false,
            CURLOPT_POST => true,
            CURLOPT_CONNECTTIMEOUT => $this->timeOut,
            CURLOPT_TIMEOUT => $this->timeOut,
            CURLOPT_RETURNTRANSFER => true,
        );
        foreach($data as $k=>$v) {
            $options = CMap::mergeArray($_options, array(
                CURLOPT_URL => $v[\'url\'],
                CURLOPT_POSTFIELDS => $v[\'requestData\'],
            ));
            if (isset($v[\'curlOptions\']) && is_array($v[\'curlOptions\']) && !empty($v[\'curlOptions\'])) {
                $options = CMap::mergeArray($options, $v[\'curlOptions\']);
            }
            $conn[$k] = curl_init();
            curl_setopt_array($conn[$k], $options);
            //add the two handles
            curl_multi_add_handle($mh, $conn[$k]);
        }
        
        $active = null;
         //execute the handles
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);

        while ($active && $mrc == CURLM_OK) {
            if (curl_multi_select($mh) != -1) {
                do {
                    $mrc = curl_multi_exec($mh, $active);
                } while ($mrc == CURLM_CALL_MULTI_PERFORM);
            }
        }
        
        $result = array();
        foreach($data as $k=>$v) {
            $result[$k] = array(
                \'curlInfo\' => curl_getinfo($conn[$k]),
                \'response\' => curl_multi_getcontent($conn[$k]),
            );
            //close the handles
            curl_multi_remove_handle($mh, $conn[$k]);
        }
        curl_multi_close($mh);
        $this->_logMultiResponse($result);
        return $result;
    }

3. Method#_handleMultiResponse()

private function _handleMultiResponse($result)
    {
        foreach($result as $k=>&$v) {
            $handleMethod = \'_handle\'. ucfirst($k);
            $v = $this->$handleMethod($v[\'response\']);
        }
        
        return $result;
    }

从处理返回方法可以看到,我们仍然需要单独去处理每一个请求的返回,并返回!

分类:

技术点:

相关文章:

  • 2021-07-05
  • 2021-09-02
  • 2021-05-27
  • 2021-05-25
  • 2021-04-01
  • 2022-01-09
猜你喜欢
  • 2022-01-07
  • 2021-12-17
  • 2021-12-08
  • 2021-11-11
  • 2021-07-26
  • 2021-08-06
  • 2021-05-07
相关资源
相似解决方案