用linux的curl实现自动投票(多进程)

接上一篇文章《用linux的curl实现自动投票 》!

 

 

为了提高投票速度,用PHP函数popen代替exec,它相当于把curl的执行推到了后台去运行,每推一次,增加一个进程。

 

<?php
/*
 *  多进程投票程序
 *  author: 木鱼
 *  blog:   http://muyu.iteye.com/
 *  
 *  关于多进程的代码参考了 回忆未来[张宴] http://blog.s135.com/post/311/
 */

/*------------获取proxy--------------*/
$result = array();

/*----------(1) mfxk.com---------*/
echo "\n";
$urlList = array(
    'http://www.mfxk.com/article/daili/54527.html',
    'http://www.mfxk.com/article/daili/54470.html',
    'http://www.mfxk.com/article/daili/54434.html',
);
foreach($urlList as $url) {
    echo "\nParsing web page: {$url}";
    $tmpHtml = file_get_contents($url);
    preg_match_all("/&nbsp;(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})&nbsp;(\d{1,4})&nbsp;/i", $tmpHtml, $matches);
    for ($i=0; $i< count($matches[0]); $i++) {
        $result[] = $matches[1][$i] . ":" . $matches[2][$i];
    }
}

/*----------(2) 996i.cn---------*/
echo "\n";
$urlList = array(
    'http://www.996i.cn/ip/sort033/7542.html',
    'http://www.996i.cn/ip/sort033/7520.html',
    'http://www.996i.cn/ip/sort033/7488.html',
    'http://www.996i.cn/ip/sort033/7487.html',
);
foreach($urlList as $url) {
    echo "\nParsing web page: {$url}";
    $tmpHtml = file_get_contents($url);
    preg_match_all("/\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+(\d{1,4})\s+/i", $tmpHtml, $matches);
    for ($i=0; $i< count($matches[0]); $i++) {
        $result[] = $matches[1][$i] . ":" . $matches[2][$i];
    }
}


/*------------去重--------------*/
$result = array_unique($result);

echo "\n\nParse excuted successfully!";
echo "\nTotal proxy:\t" . count($result);

/*------------多进程投票--------------*/
echo "\n\n\nStart to vote:";
$proxyList = $result;
while(!empty($proxyList)) {
    $proxy = array_shift($proxyList);
    run($proxy);
}
echo "\ndone!\n";


function run($proxy) {  
    global $p_number;  
    if ($p_number <= 0) {  
        $p_number = worker_processes($p_number);  
    }
    $p_number = $p_number - 1;  
    $out = popen("curl -m 30 -x {$proxy} -d \"id=1234\" http://www.example.com/VoteAction.php &", "r");
    pclose($out);
}

function worker_processes($p_number) {  
    $limit = 50;//允许推到后台的最大进程数  
    while ($p_number <= 0)  
    {  
        $cmd = popen("ps -ef | grep \"example.com\" | grep -v grep | wc -l", "r");  
        $line = fread($cmd, 512);
        pclose($cmd);
        $p_number = $limit - $line;
        if ($p_number <= 0) {
            sleep(1);//暂停1秒钟
        }
    }
    return $p_number;  
}
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值