简体   繁体   English

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

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

I'm currently using php URL to browse over 500 web pages a day with cookies. 我目前正在使用php URL每天使用Cookie浏览500多个网页。

I have to check each page to ensure that the account is still logged in and the pages are being viewed as a member, not a guest. 我必须检查每个页面,以确保该帐户仍处于登录状态,并且这些页面被视为成员而不是访客。

The script takes an hour or two to complete as it sleeps in between views. 该脚本在视图之间休眠时需要一两个小时才能完成。

I just want to know if there's anything I can do to reduce the load this script puts on the local server, I've made sure to clear variables at the end of each loop but is there anything I'm missing that would help? 我只想知道是否有什么办法可以减少此脚本在本地服务器上的负担,所以我确保在每个循环结束时清除变量,但是我缺少任何有帮助的内容吗?

Any new cURL settings that would help? 是否有任何新的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 = '';
}

You could reuse your curl handle instead of creating a new one for each connection. 您可以重复使用卷曲手柄,而不是为每个连接创建一个新的卷曲手柄。

Clearing $html at the end of each iteration won't reduce memory usage and just adds an extra operation because it already gets reset in the next iteration. 在每次迭代结束时清除$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