有什么办法加快php功能?许多卷曲请求

时间:2014-04-18 23:00:28

标签: php performance 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);

2 个答案:

答案 0 :(得分:3)

使用多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);

答案 1 :(得分:3)

多卷曲很好,但这里最重要的是加速自我卷曲

curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );

此致