PHP Multi curl 在高流量服务器集群上运行得非常慢



遇到多卷曲的速度问题。我正在使用多卷曲从各种 url 中获取 XML,所有 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 毫秒才能完成,我的客户端会丢弃该请求,因此我希望只要最长的请求需要它就运行。我将超时设置为 750ms,因为即使请求时间小于 300ms,如果低于 750ms,我的函数也不会返回任何结果。

降低这个然后curl_setopt($curly[$id],CURLOPT_TIMEOUT_MS,750);到500MS

最新更新