Iterate through the data inside the Redis queue
Then submit the data and then write the feedback back into another redis list
Code
<?PHP/** * System name:sent message * USER:GYC * DATE:2017/11/30 14:40*/ classsentmsg {functionSentaction ($data _in=NULL) { $data _in=unserialize($data _in); if(Empty($data _in[' ID ']) | |Empty($data _in[' phone ']) | |Empty($data _in[' Content '])) { return Array(' state ' =false, ' Message ' =>json_encode ($data _in));Exit; } $URL= "Http://xxxxx/sms.aspx";//operator Interface Address $data= [ ' Account ' = ' accounts ', ' pwd ' = ' password ', ' userid ' = ' xxx ', ' mobile ' =$data _in[' Phone '], ' content ' =$data _in[' content '], ' sendtime ' =NULL, ' action ' = ' send ', ' extno ' =NULL, ];//the data to be sent $ch=Curl_init (); //determine how SSL is connected if(Stripos($URL, ' https://')!==false) {curl_setopt ($ch, Curlopt_ssl_verifypeer,false); curl_setopt ($ch, Curlopt_ssl_verifyhost,false); curl_setopt ($ch, Curlopt_sslversion, 1); } $connttime= 500;//Connection Wait time 500 ms $timeout= 15000;//Timeout time 15 seconds $querystring= ""; if(Is_array($data)) { //Change data on to postable data foreach($data as $key=$val) { if(Is_array($val)) { foreach($val as $val 2) { $querystring.=UrlEncode($key) . ' = '.UrlEncode($val 2) . ' & '; } } Else { $querystring.=UrlEncode($key) . ' = '.UrlEncode($val) . ' & '; } } $querystring=substr($querystring, 0,-1);//Eliminate unnecessary &}Else { $querystring=$data; } curl_setopt ($ch, Curlopt_url,$URL); curl_setopt ($ch, Curlopt_returntransfer,true);//Feedback Informationcurl_setopt ($ch, Curlopt_http_version, curl_http_version_1_1);//HTTP version 1.1curl_setopt ($ch, Curlopt_connecttimeout_ms,$connttime);//Connection Wait Timecurl_setopt ($ch, Curlopt_timeout_ms,$timeout);//Timeout periodcurl_setopt ($ch, Curlopt_post,true); curl_setopt ($ch, Curlopt_postfields,$querystring); $file _contents= Curl_exec ($ch);//Get return value $status= Curl_getinfo ($ch); Curl_close ($ch); $call _back_json= Json_encode (simplexml_load_string($file _contents, ' SimpleXMLElement ',libxml_nocdata)); $reslut= Json_decode ($call _back_json,true); $ststus=$reslut[' ReturnStatus ']; if($ststus= = ' Success ') { return Array(' state ' =true); } Else { return Array(' state ' =false, ' Message ' =>json_encode ($reslut)); } } } Set_time_limit(0); Header("content-type:text/html; Charset=utf-8 "); $redis=NewRedis (); $redis->pconnect (' 127.0.0.1 ', 6379) or die(' Errot '); $sms=Newsentmsg (); while(true) { $data=$redis->lpop (' Queue '); if(!Empty($data)) { $res=$sms->sentaction ($data); if($res[' State ']) { $data=unserialize($data); $redis->lpush (' Queue2 ',Serialize(Array(' id ' = =$data[' ID '], ' status ' = ' success ', ' time ' = Time()))); }Else { $data=unserialize($data); $redis->lpush (' Queue2 ',Serialize(Array(' id ' = =$data[' ID '], ' status ' = ' fail ', ' time ' = Time(), ' msg ' =$res[' Message ']))); } } Sleep(Rand()% 3); }
PHP Operation Redis Send SMS