• 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('非法请求');
            }
        }
    
    
    }
  • 相关阅读:
    程序数据校验
    修改文件名后,pip命令报错:Fatal error in launcher: Unable to create process using
    算法竞赛平台
    连续子数组的元素之和最大值
    【数学计算】圆周率
    使用DOS命令关闭tomcat端口(其他服务也是可以的)
    微软project文件mpp解析
    关于读取上传文件问题的两个解决办法
    关于office在卸载了某一应用之后无法试图使用的功能所在的网络位置
    关于eclipse的一些问题
  • 原文地址:https://www.cnblogs.com/ivy-zheng/p/12457553.html
Copyright © 2020-2023  润新知