阿里巴巴刘起 發表於 2023-7-20 00:00:00

详解织梦dedecms短信验证码功能(阿里短信)

<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        现在大部分网站都需要用短信验证码,因为织梦官方没有短信验证码插件,所以写了几个短信验证码插件,一个使用的是阿里云的短信验证码接口,一个使用的是阿里大于的短信验证码接口,一个使用的是阿里通信短信验证码接口,另外一个使用的是云之讯的短信接口。下面的教程包含2个织梦短信验证码接口。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei"; text-align: center;'>
        <img style="max-width:100%!important;height:auto!important;"title="详解织梦dedecms短信验证码功能(阿里短信)" alt="详解织梦dedecms短信验证码功能(阿里短信)" src="https://zhuji.jb51.net/uploads/img/202305/66a7995747a7c1391c18f31f9d217037.jpg"></p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        <strong>织梦会员短信注册需要修改的地方:</strong><br>
        1、需要创建一个表来对验证码进行记录,防止多次发送,这里创建了一个phonecode表。<br>
        2、member/templets/reg-new.htm (注册模版添加元素)<br>
        3、member/templets/js/reg_new.js (验证手机号)<br>
        4、member/index_do.php (根据后台设置,判断是否发送注册验证码)<br>
        5、member/reg_new.php (验证、记录)</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei"; text-align: center;'>
        <font color="#428bca"><span><img style="max-width:100%!important;height:auto!important;"title="详解织梦dedecms短信验证码功能(阿里短信)" alt="详解织梦dedecms短信验证码功能(阿里短信)" src="https://zhuji.jb51.net/uploads/img/202305/d47619e88be20189a1ed5f8e3da8ee5e.jpg"></span></font></p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        <strong>a.使用阿里云短信接口接入:</strong></p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        需要在member/index_do.php对应的位置插入阿里云短信接口代码。特别需要注意的是,阿里云短信官方demo文档是大神写的,所以普通人用起来会报命名空间错误,这个需要自己注意。另外需要更新下短息模板,现在阿里短信模板审核非常严格,不允许有其他变量,之前并没有这个问题。所以,审核不过的时候,需要减少变量。(目前已经无法开通阿里云短信接口,新开通的是阿里通信接口,不能使用这段代码,只适合很久之前就开通过阿里云短信的人)</p>
<div class="jb51code" style='margin: 0px; padding: 0px; outline: none; line-height: 25.2px; font-size: 14px; width: 660px; overflow: hidden; clear: both; font-family: tahoma, arial, "Microsoft YaHei";'>
        <pre class="brush:php;toolbar:false;">
function getrandchar($length){
    $str = null;
    $strPol = "0123456789abcdefghijklmnopqrstuvwxyz";
    $max = strlen($strPol)-1;
      for($i=0;$i&lt;$length;$i++){
      $str.=$strPol;
      }
      return $str;
    }
    $code = getrandchar(5);

    require_once(DEDEINC.'/aliyun-php-sdk-core/Config.php');
    use Sms\Request\V20160927 as Sms;
    $iClientProfile = DefaultProfile::getProfile("cn-shenzhen", "your accessKey", "your accessSecret"); //登录阿里云查看: "your accessKey", "your accessSecret"
    $client = new DefaultAcsClient($iClientProfile);   
    $request = new Sms\SingleSendSmsRequest();
    $request-&gt;setSignName("<u>签名</u>");/*签名名称*/
    $request-&gt;setTemplateCode("SMS_1111");/*模板code*/
    $request-&gt;setRecNum($phone);/*目标手机号*/
    $request-&gt;setParamString("{\"code\":\"$code\",\"tel\":\"电话号码\"}");/*模板变量,请确保跟审核过的短信模版变量一致,数字一定要转换为字符串*/
    try {
      $response = $client-&gt;getAcsResponse($request);
      print_r($response);
    }
    catch (ClientException$e) {
      print_r($e-&gt;getErrorCode());
      print_r($e-&gt;getErrorMessage());
    }
    catch (ServerException$e) {
      print_r($e-&gt;getErrorCode());
      print_r($e-&gt;getErrorMessage());
    }
   
    $inquery = " INSERT INTO `dede_phonecode` (`ip`,`phone`,`phonecode`,`used`,`sendtime`) VALUES ( '$ip','$to','$code','1','$nowtime'); ";
    $rs = $dsql-&gt;ExecuteNoneQuery2($inquery);
    if( $rs = 1 ){
      ShowMsg('发送成功,请注意查收!','-1');
      exit();
    }
    exit();
</pre>
</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        附上阿里云短信模版:验证码:${code}。您正在注册,如非您本人操作,请忽略此条短信。如有疑问请与我们联系! 电话:${tel}</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        <strong>b.使用阿里大鱼的短信接口接入:</strong></p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        同样的需要在member/index_do.php对应的位置插入以下代码。</p>
<div class="jb51code" style='margin: 0px; padding: 0px; outline: none; line-height: 25.2px; font-size: 14px; width: 660px; overflow: hidden; clear: both; font-family: tahoma, arial, "Microsoft YaHei";'>
        <pre class="brush:php;toolbar:false;">
function getrandchar($length){
$str = null;
//$strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
$strPol = "0123456789abcdefghijklmnopqrstuvwxyz";
$max = strlen($strPol)-1;
for($i=0;$i&lt;$length;$i++){
$str.=$strPol;
}
return $str;
}
$code = getrandchar(5);

require_once(DEDEINC.'/alidayu-php-sdk/TopSdk.php');
$client = new TopClient;
$client -&gt;appkey = '111111' ; //登录阿里大于查看appkey。
$client -&gt;secretKey = 'aaaaaaaaaaaaaa' ; //登录阿里大于查看secret。
$req = new AlibabaAliqinFcSmsNumSendRequest;
$req -&gt;setExtend( "" );
$req -&gt;setSmsType( "normal" );
$req -&gt;setSmsFreeSignName( "城子居" ); /*签名名称*/
$req -&gt;setSmsParam( "{\"code\":\"$code\",\"name\":\"注册\"}" ); /*模板变量,请确保跟审核过的短信模版变量一致,数字一定要转换为字符串*/
$req -&gt;setRecNum($phone);/*目标手机号*/
$req -&gt;setSmsTemplateCode( "SMS_1111111" );//登录阿里大于查看/*模板ID编号*/
$resp = $client -&gt;execute( $req );

$inquery = " INSERT INTO `imm_phonecode` (`ip`,`phone`,`phonecode`,`used`,`sendtime`)VALUES ( '$ip','$to','$code','1','$nowtime'); ";
$rs = $dsql-&gt;ExecuteNoneQuery2($inquery);
if( $rs = 1 ){
ShowMsg('发送成功,请注意查收!','-1');
exit();
}
exit();
</pre>
</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        <strong>c.使用最新的阿里通信短信接口接入:</strong></p>
<div class="jb51code" style='margin: 0px; padding: 0px; outline: none; line-height: 25.2px; font-size: 14px; width: 660px; overflow: hidden; clear: both; font-family: tahoma, arial, "Microsoft YaHei";'>
        <pre class="brush:php;toolbar:false;">
function getrandchar($length){
$str = null;
//$strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
$strPol = "0123456789";
$max = strlen($strPol)-1;
for($i=0;$i&lt;$length;$i++){
$str.=$strPol;
}
return $str;
}
$number = getrandchar(4);

require_once(DEDEINC.'/aliyun-php-sdk-core/Config.php');
require_once(DEDEINC.'/Dysmsapi/Request/V20170525/SendSmsRequest.php');
require_once(DEDEINC.'/Dysmsapi/Request/V20170525/QuerySendDetailsRequest.php');

function sendSms() {
global $phone, $number;

//此处需要替换成自己的AK信息
$accessKeyId = "yourAccessKeyId";
$accessKeySecret = "yourAccessKeySecret";
//短信API产品名
$product = "Dysmsapi";
//短信API产品域名
$domain = "dysmsapi.aliyuncs.com";
//暂时不支持多Region
$region = "cn-beijing";

//初始化访问的acsCleint
$profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
DefaultProfile::addEndpoint("cn-beijing", "cn-beijing", $product, $domain);
$acsClient= new DefaultAcsClient($profile);

$request = new Dysmsapi\Request\V20170525\SendSmsRequest;
//必填-短信接收号码
$request-&gt;setPhoneNumbers($phone);
//必填-短信签名
$request-&gt;setSignName("99商铺网");
//必填-短信模板Code
$request-&gt;setTemplateCode("SMS_74725029");
//选填-假如模板中存在变量需要替换则为必填(JSON格式)
$request-&gt;setTemplateParam("{\"number\":\"$number\"}");
//选填-发送短信流水号
$request-&gt;setOutId("1234");

//发起访问请求
$acsResponse = $acsClient-&gt;getAcsResponse($request);
}

sendSms();

$inquery = " INSERT INTO `imm_phonecode` (`ip`,`phone`,`phonecode`,`used`,`sendtime`)VALUES ( '$ip','$phone','$number','1','$nowtime'); ";
$rs = $dsql-&gt;ExecuteNoneQuery2($inquery);
if( $rs = 1 ){
echo "发送成功,请注意查收!";
exit();
}
exit();
</pre>
</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        到这里阿里系的3个短信接口就全都在这里了,对应的SDK需要在阿里云官方查找下载。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        <strong>d.使用云之讯短信接口接入:</strong></p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        同样的需要在member/index_do.php对应的位置插入云之讯短信接口代码。</p>
<div class="jb51code" style='margin: 0px; padding: 0px; outline: none; line-height: 25.2px; font-size: 14px; width: 660px; overflow: hidden; clear: both; font-family: tahoma, arial, "Microsoft YaHei";'>
        <pre class="brush:php;toolbar:false;">
function getrandchar($length){
$str = null;
$strPol = "0123456789abcdefghijklmnopqrstuvwxyz";
$max = strlen($strPol)-1;
for($i=0;$i&lt;$length;$i++){
$str.=$strPol;
}
return $str;
}
require_once(DEDEINC.'/ucpaas.class.php');
$options['accountsid']='';//对应ucpaas.com用户ID
$options['token']='';//对应ucpaas.com里面的用户token
$ucpass = new Ucpaas($options);
$appId = ""; //对应ucpaas.com里面的项目ID
$to = $phone;
$templateId = ""; //对应ucpaas.com里面的短信模版ID
$code = getrandchar(5);
$param= $code.',短信模版参数2'.',短信模版参数3'; // $code为生成的验证码,短信模版参数2,短信模版参数3,参数之间用英文逗号间隔。
$ucpass-&gt;templateSMS($appId,$to,$templateId,$param);

$inquery = " INSERT INTO `dede_phonecode` (`ip`,`phone`,`phonecode`,`used`,`sendtime`) VALUES ( '$ip','$to','$code','1','$nowtime'); ";
$rs = $dsql-&gt;ExecuteNoneQuery2($inquery);
if( $rs = 1 ){
ShowMsg('发送成功,请注意查收!','-1');
exit();
}
exit();</pre>
</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        同样的附上短信模版:验证码:{1}。您正在{2},如非您本人操作,请忽略此条短信。如有疑问请与我们联系!  电话:{3}</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。</p>
頁: [1]
查看完整版本: 详解织梦dedecms短信验证码功能(阿里短信)