|
|
本帖最后由 newphp 于 2012-9-8 04:55 编辑
如果你的php开启了pcntl模块的话,可以考虑用下面的代码:- #!/usr/bin/env php
- <?php
- error_reporting(E_ALL);
- set_time_limit(0);
- ob_implicit_flush();
- $daemon = new daemon();
- switch ($_SERVER['argv'][1]) {
- case '-n':
- $daemon->start(FALSE);
- break;
- case 'start':
- $daemon->start();
- break;
- case 'stop':
- $daemon->stop();
- break;
- case 'forcestop':
- $daemon->stop(SIGKILL);
- break;
- default:
- $daemon->tips();
- break;
- }
- function main_action ()
- {
- $phpqq = new phpqq();
- $phpqq->load_config(dirname(__FILE__) . '/sids_data.php');
- $phpqq->loop();
- }
- class phpqq
- {
- private $http_status; // 记录请求返回状态
- private $freq_http = 500; // 向腾迅请求的最高频率,单位毫秒
- private $last_request_time = 0; // 记录上次向腾迅请求的时间
- private $last_login_time = 0; //记录上次统一登陆的时间
- private $freq_login; //登陆频率
-
- private $sids_file = './sids_data.php'; // 配置文件位置
- private $sids_file_mtime = 0; // 配置文件最后修改时间
- private $sid_array = array(); // sid数组
- private $addon_sids = array(); // 新添加的sid
-
- public function __construct ($freq_login = 1800)
- {
- $this->freq_login = $freq_login;
- }
-
- public function load_config ($sids_file = '')
- {
- if ($sids_file != '') {
- $this->sids_file = $sids_file;
- }
- if (file_exists($this->sids_file)) {
- require $this->sids_file;
- }
- if ($this->sids_file_mtime > 0) {
- foreach ($sid_array as $sid) {
- if (! in_array($sid, $this->sid_array)) {
- $this->addon_sids[] = $sid;
- }
- }
- }
- $this->sid_array = $sid_array;
- if (! is_array($this->sid_array)) {
- $this->sid_array = array();
- $this->write_config();
- }
- clearstatcache();
- $this->sids_file_mtime = filemtime($this->sids_file);
- }
-
- public function loop ()
- {
- while (true) {
- // 当配置文件被修改时,重新载入
- clearstatcache();
- if (filemtime($this->sids_file) > $this->sids_file_mtime) {
- $this->load_config($this->sids_file);
- }
-
- // 定时登陆
- $curr_time = time();
- if ($curr_time - $this->last_login_time > $this->freq_login) {
- foreach ($this->sid_array as $k => $sid) {
- $return = $this->doLogin($sid);
- if ($return === FALSE) {
- $this->sid_array[$k] = '';
- $this->sid_array = array_filter($this->sid_array);
- $this->write_config();
- $this->log("sid: " . $sid . " 失效\n");
- }
- else {
- list ($qqnum, $qqnickname) = $return;
- $this->log("sid: " . $sid . " 登陆成功。 QQ: " . $qqnum . " 昵称:" . $qqnickname . "\n\n");
- }
- }
- $this->last_login_time = $curr_time;
- }
-
- // 当新添加sid时,马上就登陆一次
- foreach ($this->addon_sids as $sid1) {
- $return = $this->doLogin($sid1);
- if ($return === FALSE) {
- foreach ($this->sid_array as $k => $sid2) {
- if ($sid1 == $sid2) {
- $this->sid_array[$k] = '';
- $this->sid_array = array_filter($this->sid_array);
- $this->write_config();
- $this->log("sid: " . $sid1 . " 失效\n");
- }
- else {
- list ($qqnum, $qqnickname) = $return;
- $this->log("sid: " . $sid1 . " 登陆成功。 QQ: " . $qqnum . " 昵称:" . $qqnickname . "\n\n");
- }
- }
- }
- }
- // 清空新添加的sid
- $this->addon_sids = array();
-
- sleep(1);
- }
- }
-
- private function write_config ()
- {
- $fp = fopen($this->sids_file, "w+");
- fwrite($fp, "<?php\n\$sid_array = " . var_export($this->sid_array, TRUE) . ";");
- fclose($fp);
- }
-
- private function doLogin ($sid, $sleep_utime = 1000)
- {
- $login_url = "http://pt.3g.qq.com/s?aid=nLogin3gqqbysid&r=" . $this->randomFloat();
- $postdata = array();
- $postdata['auto'] = "1";
- $postdata['loginType'] = "1";
- $postdata['3gqqsid'] = $sid;
- $return_html = $this->curl_request($login_url, $postdata);
- if ($this->http_status != 200) {
- return FALSE;
- }
- if (preg_match("/3G_UIN=(\d+)/", $return_html, $matchs1)) {
- preg_match("/ (.*?) /", $return_html, $matchs2);
- $return_array = array_merge($matchs1, $matchs2);
- unset($return_html);
- return array($matchs1[1], $matchs2[1]);
- }
- else {
- return FALSE;
- }
- }
- private function curl_request ($source_url, $postdata = array())
- {
- if ((microtime(TRUE) - $this->last_request_time) < ($this->freq_http / 1000)) {
- usleep($this->freq_http);
- }
-
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $source_url);
- curl_setopt($ch, CURLOPT_HEADER, FALSE);
- curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
- curl_setopt($ch, CURLOPT_TIMEOUT, 60);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
- curl_setopt($ch, CURLOPT_USERAGENT,
- "BlackBerry8820/4.5.0.182 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/102"); //我手机的User-Agent
- if (! empty($postdata)) {
- $postdata = http_build_query($postdata);
- curl_setopt($ch, CURLOPT_POST, TRUE);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
- }
- $return_html = curl_exec($ch);
- $curl_info = curl_getinfo($ch);
- curl_close($ch);
- $this->http_status = $curl_info['http_code'];
- unset($source_url, $ch, $curl_info);
-
- $this->last_request_time = microtime(TRUE);
-
- return $return_html;
- }
-
- private function randomFloat ($min = 0, $max = 1)
- {
- return $min + mt_rand() / mt_getrandmax() * ($max - $min);
- }
-
- private function log ($text)
- {}
- }
- class daemon
- {
-
- private $pidfile;
-
- function __construct ()
- {
- $this->pidfile = '/tmp/' . str_replace(".", "_", basename(__FILE__)) . '.pid';
- }
-
- function tips ()
- {
- echo "\nUsage: " . $_SERVER['_'] . " " . __FILE__ . " [start|stop]\n\n";
- exit(2);
- }
-
- function stop ($sig = SIGTERM)
- {
- // 判断进程pid文件是否存在
- if (! file_exists($this->pidfile)) {
- echo "PHP daemon is not running.\n";
- exit(0);
- }
- else {
- $pid = intval(file_get_contents($this->pidfile));
- // 判断进程是否存在
- if (! file_exists('/proc/' . $pid)) {
- echo "PHP daemon is not running.\n";
- @unlink($this->pidfile);
- exit(0);
- }
- // 中止进程
- $kill_result = posix_kill($pid, $sig);
- usleep(500);
- if ($kill_result) { // 中止进程成功
- if (file_exists('/proc/' . $pid)) { //检查进程是否还存在
- echo "PHP daemon stop failed.\n";
- exit(0);
- }
- else {
- echo "PHP daemon stopped.\n";
- @unlink($this->pidfile);
- exit(0);
- }
- }
- else { // 中止进程失败
- echo "PHP daemon stop failed.\n";
- exit(1);
- }
- }
- }
-
- function start ($daemon = TRUE)
- {
- $pid = pcntl_fork();
- if ($pid == - 1) {
- die('could not fork');
- }
- else {
- if ($pid) {
- // we are the parent
- if ($daemon === FALSE) {
- pcntl_wait($status);
- }
- }
- else {
- $fork_pid = posix_getpid();
- $length = @file_put_contents($this->pidfile, $fork_pid);
- if ($length !== FALSE) {
- echo "PHP daemon start successfull.\n";
- posix_setgid(getmygid());
- posix_setuid(getmyuid());
- main_action();
- }
- echo "PHP daemon start failed.\n";
- @unlink($this->pidfile);
- }
- }
- }
- }
复制代码 如果有pcntl,代码加上可执行权chmod +x name.php
然后直接可以用/path/to/name.php start执行,或者放到/etc/rc.local里开始自动执行
判断有没有pcntl,执行如果没有输出那就代表不支持
如果没有pcntl的话,把daemon类去掉,并且把- $daemon = new daemon();
- switch ($_SERVER['argv'][1]) {
- case '-n':
- $daemon->start(FALSE);
- break;
- case 'start':
- $daemon->start();
- break;
- case 'stop':
- $daemon->stop();
- break;
- case 'forcestop':
- $daemon->stop(SIGKILL);
- break;
- default:
- $daemon->tips();
- break;
- }
复制代码 换成然后用nohup来执行- nohup /path/to/name.php > /dev/null 2>&1 &
复制代码 void log ($text)方法自己写成你自己想要的。 |
评分
-
查看全部评分
|