有什么方法可以加快php函数的速度吗?许多卷曲请求


any way to speed up php function? many curl requests

检查链接是否可用的Curl函数重复了大约600次,大约需要20分钟,我记得一年前做了一些事情,它在30秒的中开始做同样的事情

//return the availability of link
function check($url){
$agent = 'Mozilla/4.0 (compatible;)';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url );
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 60000);
curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 60000);
curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:11107');
    // set start time
    $mtime = microtime(); 
    $mtime = explode(' ', $mtime); 
    $mtime = $mtime[1] + $mtime[0]; 
    $starttime = $mtime; 
$page = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo curl_error($ch); echo '<br />';
    // set end time
    $mtime = microtime(); 
    $mtime = explode(" ", $mtime); 
    $mtime = $mtime[1] + $mtime[0]; 
    $endtime = $mtime; 
        $totaltime = ($endtime - $starttime);
curl_close($ch);
echo "This script executed in " .$totaltime. " seconds. With ".$httpcode." Status.";
if($httpcode>=100 && $httpcode<600) return $httpcode; else return 0;
}

我用调用这个函数

$new_pr   = check($url);

使用多cURL处理程序。

我还注意到您有一个非常高的超时值。如果网站真的需要那么长时间来响应,这将导致速度下降。当心

$opts = array(CURLOPT_HEADER => true, CURLOPT_TIMEOUT => 30, CURLOPT_NOBODY => true, CURLOPT_USERAGENT => $agent, CURLOPT_FAILONERROR => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_CONNECTTIMEOUT => 60, CURLOPT_PROXY => '127.0.0.1:11107');
$mh = curl_multi_init();
$urls = array('http://url.com', 'etc');
foreach($urls as $url) {
    $ch = curl_init($url);
    curl_setopt_array($ch, $opts);
    curl_multi_add_handle($mh, $ch);
}
$results = array();
do {
    while(($exec = curl_multi_exec($mh, $running)) == CURLM_CALL_MULTI_PERFORM);
    if($exec != CURLM_OK) {
        break;
    }
    while($ch = curl_multi_info_read($mh)) {
        $j++;
        $ch = $ch['handle'];
        $error = curl_error($ch);
        if(!$error) {
            $results = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        } else {
            $results[] = $error;
        }
        curl_multi_remove_handle($mh, $ch);
        curl_close($ch);                
    }
} while($running);
curl_multi_close($mh);

多卷曲是好的,但这里最重要的是加快卷曲速度

curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );

问候