Created
October 31, 2012 07:28
-
-
Save kadashu/3985608 to your computer and use it in GitHub Desktop.
sms daemon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
date_default_timezone_set('Asia/Shanghai'); | |
$sleeptime = 1; | |
require 'PHPFetion.php'; | |
#read to and msg from redis | |
function read_msg() | |
{ | |
global $sleeptime; | |
$redis = new Redis(); | |
try { | |
$redis->pconnect('127.0.0.1', 6379); | |
#return value: ARRAY array('listName', 'element') | |
#$sms_raw = $redis->blPop('sms', 1); | |
$sms_raw = $redis->rPop( 'sms' ); | |
echo "CountOfSmsRaw:".count($sms_raw).";Sms_raw:".$sms_raw."\n"; | |
#if ( count($sms_raw) == 2 ){ | |
if ( $sms_raw ){ | |
# decode | |
$user = explode( ',', $sms_raw )[0]; | |
$pass = explode( ',', $sms_raw )[1]; | |
$to = explode( ',', $sms_raw )[2]; | |
$msg = explode( ',', $sms_raw )[3]; | |
$sleeptime = 1; | |
$cur_msg = array( $user, $pass, $to, $msg ); | |
} | |
else{ | |
$sleeptime = 30; | |
return null; | |
} | |
} catch(Exception $e) { | |
echo "Date:".date('Y/m/d H:i:s', time()).";ERROR:".$e."\n"; | |
$sleeptime = 30; | |
} | |
$redis->close(); | |
return $cur_msg; | |
} | |
# daemon to send msg to mobile | |
while(1){ | |
sleep( $sleeptime ); | |
$cur_msg = read_msg(); | |
echo "CountOfRead_msg:".count($cur_msg)."\n"; | |
if ( count($cur_msg) == 4 ){ | |
$fetion= new PHPFetion( "$cur_msg[0]", "$cur_msg[1]" ); | |
$fetion->send( "$cur_msg[2]", "$cur_msg[3]" ); | |
echo "NextSleep:".$sleeptime.";Date:".date('Y/m/d H:i:s', time()).";User:".$cur_msg[0].";To:".$cur_msg[2].";Msg:".$cur_msg[3].";Finished\n"; | |
} | |
else{ | |
echo "NextSleep:".$sleeptime."\n"; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment