繁体   English   中英

减少php cURL放在服务器上的负载

[英]Reduce the load php cURL puts on the server

我目前正在使用php URL每天使用Cookie浏览500多个网页。

我必须检查每个页面,以确保该帐户仍处于登录状态,并且这些页面被视为成员而不是访客。

该脚本在视图之间休眠时需要一两个小时才能完成。

我只想知道是否有什么办法可以减少此脚本在本地服务器上的负担,所以我确保在每个循环结束时清除变量,但是我缺少任何有帮助的内容吗?

是否有任何新的cURL设置会有所帮助?

$i = 0;
$useragents = array();

foreach($urls as $url){

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_COOKIEJAR, str_replace('\\','/',dirname(__FILE__)).'/cookies.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, str_replace('\\','/',dirname(__FILE__)).'/cookies.txt');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, $useragents[array_rand($useragents)]);

    $html = curl_exec($ch);
    curl_close($ch);

    if(!$html)
        die("No HTML - Not logged in");

    if($i %10 != 0)
        sleep(rand(5,20));
    else
        sleep(rand(rand(60,180), rand(300,660)));

    $i++;

    $html = '';
}

您可以重复使用卷曲手柄,而不是为每个连接创建一个新的卷曲手柄。

在每次迭代结束时清除$html不会减少内存使用量,只会增加额外的操作,因为它已在下一次迭代中重置。

$i = 0;
$useragents = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, str_replace('\\','/',dirname(__FILE__)).'/cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, str_replace('\\','/',dirname(__FILE__)).'/cookies.txt');

foreach($urls as $url){

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, $useragents[array_rand($useragents)]);
    $html = curl_exec($ch);

    if(!$html)
        die("No HTML - Not logged in");

    if($i++ % 10 != 0)
        sleep(rand(5,20));
    else
        sleep(rand(rand(60,180), rand(300,660)));  
}

curl_close($ch);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM