zoukankan      html  css  js  c++  java
  • PHP SDK短信接口

    一、代码

    class Sdkmessage
    {
        private $config;
    
        /**
         * sdk 短信接口
         * @param $tel 手机号
         * @param $content  短信内容
         * @return bool
         */
        public function send($tel, $content)
        {
            $content = "【给力餐饮】您的短信验证码为:".$content;
            $flag         = 0;
            $params       = '';
            $this->config = config('sdk_sms');
            //要post的数据
            $argv = array(
                'sn'      => $this->config['sn'], ////替换成您自己的序列号
                'pwd'     => strtoupper(md5($this->config['sn'] . $this->config['pwd'])), //此处密码需要加密 加密方式为 md5(sn+password) 32位大写
                'mobile'  => $tel,//手机号 多个用英文的逗号隔开 post理论没有长度限制.推荐群发一次小于等于10000个手机号
                'content' => iconv("UTF-8", "gb2312//IGNORE", $content),//短信内容
                'ext'     => '',
                'stime'   => '',//定时时间 格式为2011-6-29 11:09:21
                'rrid'    => ''
            );
            //构造要post的字符串
            foreach ($argv as $key => $value) {
                if ($flag != 0) {
                    $params .= "&";
                    $flag   = 1;
                }
                $params .= $key . "=";
                $params .= urlencode($value);
                $flag   = 1;
            }
            $length = strlen($params);
            //创建socket连接
            $fp = fsockopen("sdk.entinfo.cn", 8060, $errno, $errstr, 10) or exit($errstr . "--->" . $errno);
            //构造post请求的头
            $header = "POST /webservice.asmx/mt HTTP/1.1
    ";
            $header .= "Host:sdk.entinfo.cn
    ";
            $header .= "Content-Type: application/x-www-form-urlencoded
    ";
            $header .= "Content-Length: " . $length . "
    ";
            $header .= "Connection: Close
    
    ";
            //添加post的字符串
            $header .= $params . "
    ";
            //发送post的数据
            fputs($fp, $header);
            $inheader = 1;
            while (!feof($fp)) {
                $line = fgets($fp, 1024); //去除请求包的头只显示页面的返回数据
                if ($inheader && ($line == "
    " || $line == "
    ")) {
                    $inheader = 0;
                }
                if ($inheader == 0) {
                    // echo $line;
                }
            }
    //        dump($line);
            //<string xmlns="http://tempuri.org/">-5</string>
            $line   = str_replace("<string xmlns="http://tempuri.org/">", "", $line);
            $line   = str_replace("</string>", "", $line);
            $result = explode("-", $line);
    //        dump($result);
            // echo $line."-------------";
            if (count($result) > 1)
                return ['code'=>'ERROR','message'=>'发送失败'];
            else
                return ['code'=>'OK','message'=>'发送成功'];
        }
    
    }

    二、调用

    class Message extends Controller
    {
        const TYPE = 'Sdkmessage';//Alimessage
        public $obj;
    
        /**
         * @api {post} /mobile/message/send 发送短信
         * @apiName  /message/send
         * @apiGroup 短信验证
         * @apiParam {string} mobile 手机号
         * @apiSuccess {String} status 状态值:{'ok':成功,'error':失败,'msg':提示信息}
         **/
        public function send()
        {
            try {
                if (!request()->isPost()) {
                    return tips('非法请求');
                }
                $mobile = input('mobile', '', 'trim');
                if (empty($mobile)) return tips('手机号不能为空');
                $cust = db('customer')->where('tel', $mobile)->where('status', 1)->find();
                if (empty($cust)) {
                    return tips('手机号不存在,请重新输入');
                }
    
                $class     = "\message\" . self::TYPE;
                $this->obj = new $class();
    
                $code = rand(100000, 999999);
                $data = [
                    'content'  => $code,
                    'mobile'   => $mobile,
                    'type'     => 0,
                    'status'   => 1,
                    'create_time' => time()
                ];
    
                $id = db('mobile_apply')->insert($data);
    
                //发送短信
                $res = $this->obj->send($mobile, $code);
                if ($res['code'] == 'OK') {
                    db('mobile_apply')->where('status', $id)
                        ->update(['status' => 2, 'msg' => json($res['message'])]);
                    return tips('发送成功', 1, $res);
                } else {
                    return tips($res['message']);
                }
            }catch(Exception $e)
            {
                return tips($e->getMessage());
            }
    
        }
    
        /**
         * @api {post} /mobile/message/valid 验证
         * @apiName  /message/valid
         * @apiGroup 短信验证
         * @apiParam {string} mobile 手机号
         * @apiParam {string} code 验证码
         * @apiSuccess {String} status 状态值:{'ok':成功,'error':失败,'msg':提示信息}
         **/
        public function valid()
        {
            Session::prefix('index_auth');
            if (request()->isPost()) {
                try {
                    $mobile = input('mobile', '', 'trim');
                    $code   = input('code', '', 'trim');
    
                    $sms          = db('mobile_apply')
                        ->where(['mobile' => $mobile, 'type' => ['eq', 0], 'status' => 2])
                        ->order('create_time desc')
                        ->find();
    
                    if (!$sms) {
                        throw new Exception('短信验证码已过期');
                    }
    //                dump($sms);
                    if ((time() - $sms['create_time']) > $this->config['expireTime']) {
                        throw new Exception('短信验证码已经过期');
                    }
    
                    if ($code == $sms['content']) {
                        db('mobile_apply')->where(['mobile' => $mobile, 'type' => ['eq', 0]])->delete();
                        //查询用户的信息
                        $info = db('cust_account')->where(array('tel' => $mobile, 'status' => 1))->order('create_time desc')->find();
                        session('tel', $mobile);
                        session('name', $info['account']);
                        session('user_id', $info['cust_id']);//
                        session('cid', $info['id']);
                        return tips('验证成功', 1);
                    } else {
                        return tips('验证失败');
                    }
                } catch (Exception $e) {
                    return tips($e->getMessage());
                }
            } else {
                return tips('非法请求');
            }
        }
    
    
    }
  • 相关阅读:
    linux 解压tgz 文件指令
    shell 脚本没有执行权限 报错 bash: ./myshell.sh: Permission denied
    linux 启动solr 报错 Your Max Processes Limit is currently 31202. It should be set to 65000 to avoid operational disruption.
    远程查询批量导入数据
    修改 MZTreeView 赋权节点父节点选中子节点自动选中的问题
    关于乱码的问题解决记录
    我的网站优化之路
    对设计及重构的一点反思
    我的五年岁月
    奔三的路上
  • 原文地址:https://www.cnblogs.com/ivy-zheng/p/12457553.html
Copyright © 2011-2022 走看看