1

遇到多卷曲的速度问题。我正在使用 multi curl 从各种 url 中获取 XML,所有响应时间都低于 300 毫秒。但是我的多卷曲功能需要 1 秒以上的时间来获取这些 URL(仅大约 10-15 个 URL)。下面是我正在使用的代码:

function multiRequest($data, $options = array()) {

    // array of curl handles
    $curly = array();
    // data to be returned
    $result = array();

    // multi handle
    $mh = curl_multi_init();

    // loop through $data and create curl handles
    // then add them to the multi-handle
    foreach ($data as $id => $d) {

    $curly[$id] = curl_init();

    $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
    curl_setopt($curly[$id], CURLOPT_URL,            $url);
    curl_setopt($curly[$id], CURLOPT_HEADER,         0);
    curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curly[$id], CURLOPT_NOSIGNAL, 1);
    curl_setopt($curly[$id], CURLOPT_TIMEOUT_MS, 750);


    curl_multi_add_handle($mh, $curly[$id]);
    }

    // execute the handles
    do {
        curl_multi_select($mh, .01);
        $status = curl_multi_exec($mh, $running);
    } while ($status === CURLM_CALL_MULTI_PERFORM || $running);


    // get content and remove handles
    foreach($curly as $id => $c) {
        if(curl_errno($c) == 0)
            $result[$id] = curl_multi_getcontent($c);
        curl_multi_remove_handle($mh, $c);
    }

    // all done
    curl_multi_close($mh);

    return $result;
}

我应该做些什么来加快速度吗?如果请求需要超过 500 毫秒才能完成,我的客户会丢弃该请求,所以我想让它运行,只要最长的请求需要。我将超时设置为 750 毫秒,因为即使请求时间小于 300 毫秒,如果低于 750 毫秒,我的函数也不会返回任何结果。

4

1 回答 1

-1

然后降低它 curl_setopt($curly[$id], CURLOPT_TIMEOUT_MS, 750); 至 500 毫秒

于 2013-12-11T05:12:38.937 回答