zoukankan      html  css  js  c++  java
  • php 操作Redis发送短信

    循环查询redis队列里面的数据

    然后提交数据后将反馈信息再写入另一个 redis list里面

    代码

    <?php  
      
    /** 
     * System Name: sent message 
     * User: gyc 
     * Date: 2017/11/30 14:40 
     */  
    class Sentmsg  
    {  
        function sentaction($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";//运营商接口地址 
            $data =  
                [  
                    'account' => '账号',  
                    'pwd' => '密码',  
                    'userid' => 'xxx',  
                    'mobile' => $data_in['phone'],  
                    'content' =>  $data_in['content'],  
                    'sendTime' => null,  
                    'action' => 'send',  
                    'extno' => null,  
                ];//要发送的数据  
            $ch = curl_init();  
            //判断ssl连接方式  
            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; //连接等待时间500毫秒  
            $timeout = 15000;//超时时间15秒  
      
            $querystring = "";  
            if (is_array($data)) {  
                // Change data in to postable data  
                foreach ($data as $key => $val) {  
                    if (is_array($val)) {  
                        foreach ($val as $val2) {  
                            $querystring .= urlencode($key) . '=' . urlencode($val2) . '&';  
                        }  
                    } 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);//反馈信息  
            curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); //http 1.1版本  
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $connttime);//连接等待时间  
            curl_setopt($ch, CURLOPT_TIMEOUT_MS, $timeout);//超时时间  
            curl_setopt($ch, CURLOPT_POST, true);  
            curl_setopt($ch, CURLOPT_POSTFIELDS, $querystring);  
            $file_contents = curl_exec($ch);//获得返回值  
            $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 = new Redis();  
    $redis->pconnect('127.0.0.1', 6379) or die('errot');  
    $sms = new Sentmsg();  
    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);  
    } 
  • 相关阅读:
    批量杀掉多个pid文件中记录的pid进程, 并集成到shell脚本中
    把tomcat服务器配置为windows服务的方法
    idea导入java项目
    linux-umount挂载点无法卸载:device is busy(解决)
    简单(基本)的风光摄影照片后期处理-新手教程-ps照片后期基本处理
    golang 打包,交叉编译,压缩
    mac下Fiddler的安装-启动
    修改ElementUI源码样式
    linux里面的命令:./和. /(这里有空格)的区别
    linux下mysql源码安装
  • 原文地址:https://www.cnblogs.com/guoyachao/p/7930738.html
Copyright © 2011-2022 走看看