PHP Multi-Curl在高流量服务器集群上运行非常慢
遇到多重卷曲的速度问题。我使用multi-curl从各种URL获取XML,所有这些URL的响应时间都在300毫秒以下。但是我的multi-curl函数需要花费1秒来获取这些URL(仅10-15个URL)。下面是我正在使用的代码:PHP Multi-Curl在高流量服务器集群上运行非常慢,php,curl,curl-multi,Php,Curl,Curl Multi,遇到多重卷曲的速度问题。我使用multi-curl从各种URL获取XML,所有这些URL的响应时间都在300毫秒以下。但是我的multi-curl函数需要花费1秒来获取这些URL(仅10-15个URL)。下面是我正在使用的代码: function multiRequest($data, $options = array()) { // array of curl handles $curly = array(); // data to be returned $
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\u setopt($curly[$id],CURLOPT\u timeout\u MS,750);至500毫秒