九月丶

阿里云最新版发送短信接口实现类
/** * 发送短信验证码 * @author 命运 <...
扫描右侧二维码阅读全文
04
2018/01

阿里云最新版发送短信接口实现类

        /**
         * 发送短信验证码
         * @author 命运 <destiny1123@aliyun.com>
         * @return mixed
         */
        public function sms()
        {
            if(request()->isPost()){
                $Send = new Send;
    
                $mobile = input('post.mobile/s','','trim,strip_tags');
    
                $code = get_rand_num(6);
    
                //加入缓存进行验证
                cache($mobile, $code, 600);
                // echo $data['param']['code'];
             
                $result = $Send->sms([
                    'param'  => ['code'=>$code,'product'=>'法律通'],
                    'mobile'  => $mobile,
                    'template'  => 'SMS_67265806',
                    'FreeSignName' => '注册验证',//短信签名
                ]);
              
                if($result !== true){
                    
                    return $this->error($Send->getErrorMessage($result));
                }
                return $this->success('短信发送成功,请注意查收!');
            }
        }
    

下面是发送短信的实现类

<?php
namespace app\common\model;

use think\Model;
use think\Validate;

class Send extends Model
{
    public static $sms_config = [
        'appkey'    => 'appkey',//阿里大于APPKEY
        'secretKey' => 'secretkey',//阿里大于secretKey
        ];
    public function sms($data=[])
    {
        $validate = new Validate([
            ['param','require|array','参数必填|参数必须为数组'],
            ['mobile','require|/1[34578]{1}\d{9}$/','手机号错误|手机号错误'],
            ['template','require','模板id错误'],
            ['FreeSignName','require','短信签名错误']
            ]);
        if (!$validate->check($data)) {
            return $validate->getError();
        }
        
        define('TOP_SDK_WORK_DIR', CACHE_PATH.'sms_tmp/');
        define('TOP_SDK_DEV_MODE', false);
        vendor('alidayu.TopClient');
        vendor('alidayu.AlibabaAliqinFcSmsNumSendRequest');
        vendor('alidayu.RequestCheckUtil');
        vendor('alidayu.ResultSet');
        vendor('alidayu.TopLogger');
        $config = self::$sms_config;
        $c = new \TopClient;
        $c->appkey = $config['appkey'];
        $c->secretKey = $config['secretKey'];
        $req = new \AlibabaAliqinFcSmsNumSendRequest;
        $req->setExtend('');
        $req->setSmsType('normal');
        $req->setSmsFreeSignName($data['FreeSignName']);
        $req->setSmsParam(json_encode($data['param']));
        $req->setRecNum($data['mobile']);
        $req->setSmsTemplateCode($data['template']);
        $result = $c->execute($req);
        $result = $this->_simplexml_to_array($result);

        if(isset($result['code'])){
            return $result['sub_code'];
        }
        return true;
    }

    private function _simplexml_to_array($obj)
    {
        if(count($obj) >= 1){
        $result = $keys = [];
        foreach($obj as $key=>$value){
            isset($keys[$key]) ? ($keys[$key] += 1) : ($keys[$key] = 1);
            if( $keys[$key] == 1 ){
                $result[$key] = $this->_simplexml_to_array($value);
                }elseif( $keys[$key] == 2 ){
                    $result[$key] = [$result[$key], $this->_simplexml_to_array($value)];
                }else if( $keys[$key] > 2 ){
                    $result[$key][] = $this->_simplexml_to_array($value);
                }
            }
            return $result;
        }else if(count($obj) == 0){
            return (string)$obj;
            }
    }

    /**
      * 获取详细错误信息
      * @param string $status
      * @return string 返回中文错误信息
      */
     public function getErrorMessage($status) {

         $message = array(
            'isp.RAM_PERMISSION_DENY' => 'RAM权限不足',
            'isv.OUT_OF_SERVICE' => '业务停机',
            'isv.PRODUCT_UN_SUBSCRIPT' => '未开通云通信产品的阿里云客户',
            'isv.PRODUCT_UNSUBSCRIBE' => '产品未开通',
            'isv.ACCOUNT_NOT_EXISTS' => '账户不存在',
            'isv.ACCOUNT_ABNORMAL' => '账户异常',
            'isv.SMS_TEMPLATE_ILLEGAL' => '短信模板不合法',
            'isv.SMS_SIGNATURE_ILLEGAL' => '短信签名不合法',
            'isv.INVALID_PARAMETERS' => '参数异常',
            'sp.SYSTEM_ERROR' => 'isp.SYSTEM_ERROR',
            'isv.MOBILE_NUMBER_ILLEGAL' => '非法手机号',
            'isv.MOBILE_COUNT_OVER_LIMIT' => '手机号码数量超过限制',
            'isv.TEMPLATE_MISSING_PARAMETERS' => '模板缺少变量',
            'isv.BUSINESS_LIMIT_CONTROL' => '短信发送频繁,请稍后重试',
            'isv.INVALID_JSON_PARAM' => 'JSON参数不合法,只接受字符串值',
            'isv.BLACK_KEY_CONTROL_LIMIT' => '非法关键字',
            'isv.PARAM_LENGTH_LIMIT' => '参数超出长度限制    ',
            'isv.PARAM_NOT_SUPPORT_URL' => '不支持URL',
            'isv.AMOUNT_NOT_ENOUGH' => '账户余额不足',
            'isv.TEMPLATE_PARAMS_ILLEGAL' => '模板变量里包含非法关键字',
            'SignatureDoesNotMatch' => 'Specified signature is not matched with our calculation.',
            'InvalidTimeStamp.Expired' => 'Specified time stamp or date value is expired.',
            'SignatureNonceUsed' => 'Specified signature nonce was used already.',
            'InvalidVersion' => 'Specified parameter Version is not valid.',
             'InvalidAction.NotFound' => 'Specified api is not found, please check your url and method',
            );
        if (isset ( $message [$status] )) {
            return $message [$status];
        }
        return $status;
    }
}
?>
最后修改:2018 年 01 月 30 日 11 : 03 AM
如果觉得我的文章对你有用,请随意赞赏

发表评论