OSS\Http\RequestCore::send_multi_request PHP Метод

send_multi_request() публичный Метод

Sends the request using , enabling parallel requests. Uses the "rolling" method.
public send_multi_request ( array $handles, array $opt = null ) : array
$handles array (Required) An indexed array of cURL handles to process simultaneously.
$opt array (Optional) An associative array of parameters that can have the following keys:
  • callback - string|array - Optional - The string name of a function to pass the response data to. If this is a method, pass an array where the [0] index is the class and the [1] index is the method name.
  • limit - integer - Optional - The number of simultaneous requests to make. This can be useful for scaling around slow server responses. Defaults to trusting cURLs judgement as to how many to use.
Результат array Post-processed cURL responses.
    public function send_multi_request($handles, $opt = null)
    {
        set_time_limit(0);
        // Skip everything if there are no handles to process.
        if (count($handles) === 0) {
            return array();
        }
        if (!$opt) {
            $opt = array();
        }
        // Initialize any missing options
        $limit = isset($opt['limit']) ? $opt['limit'] : -1;
        // Initialize
        $handle_list = $handles;
        $http = new $this->request_class();
        $multi_handle = curl_multi_init();
        $handles_post = array();
        $added = count($handles);
        $last_handle = null;
        $count = 0;
        $i = 0;
        // Loop through the cURL handles and add as many as it set by the limit parameter.
        while ($i < $added) {
            if ($limit > 0 && $i >= $limit) {
                break;
            }
            curl_multi_add_handle($multi_handle, array_shift($handles));
            $i++;
        }
        do {
            $active = false;
            // Start executing and wait for a response.
            while (($status = curl_multi_exec($multi_handle, $active)) === CURLM_CALL_MULTI_PERFORM) {
                // Start looking for possible responses immediately when we have to add more handles
                if (count($handles) > 0) {
                    break;
                }
            }
            // Figure out which requests finished.
            $to_process = array();
            while ($done = curl_multi_info_read($multi_handle)) {
                // Since curl_errno() isn't reliable for handles that were in multirequests, we check the 'result' of the info read, which contains the curl error number, (listed here http://curl.haxx.se/libcurl/c/libcurl-errors.html )
                if ($done['result'] > 0) {
                    throw new RequestCore_Exception('cURL resource: ' . (string) $done['handle'] . '; cURL error: ' . curl_error($done['handle']) . ' (' . $done['result'] . ')');
                } elseif (!isset($to_process[(int) $done['handle']])) {
                    $to_process[(int) $done['handle']] = $done;
                }
            }
            // Actually deal with the request
            foreach ($to_process as $pkey => $done) {
                $response = $http->process_response($done['handle'], curl_multi_getcontent($done['handle']));
                $key = array_search($done['handle'], $handle_list, true);
                $handles_post[$key] = $response;
                if (count($handles) > 0) {
                    curl_multi_add_handle($multi_handle, array_shift($handles));
                }
                curl_multi_remove_handle($multi_handle, $done['handle']);
                curl_close($done['handle']);
            }
        } while ($active || count($handles_post) < $added);
        curl_multi_close($multi_handle);
        ksort($handles_post, SORT_NUMERIC);
        return $handles_post;
    }

Usage Example

Пример #1
0
 public function testSendMultiRequest()
 {
     $httpCore = new RequestCore("http://www.baidu.com");
     $ch1 = curl_init("http://www.baidu.com");
     curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
     $ch2 = curl_init("http://cn.bing.com");
     curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
     @($result = $httpCore->send_multi_request(array($ch1, $ch2)));
     $this->assertNotNull($result);
 }
All Usage Examples Of OSS\Http\RequestCore::send_multi_request