ASP版_阿里大于短信API Demo
阿里大于申请地址:http://www.alidayu.com
阿里大于短信发送Demo:
******index.asp*************
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!--#include file="Alidayu_sms_send.asp" -->
<%
Dim Code,Mobile
Dim Sms,result,SMSParam
Code = MakeRandNum()
Session("code") = Code
Mobile = Request("mobile")
SMSParam = Array("number="&Code)'短信模板变量,传参规则Array{"key=value","key1=value1"},key的名字须和申请模板中的变量名一致,
If Mobile<>"" then
Set Sms = New Alidayu_sms_send
Sms.AppKey = "23631215" '阿里大鱼分配给你的AppKey
Sms.Secret = "96d77a7c4d9185feecf682d92128318b"'AppKey对应的Secret
Sms.SignName = "阿里大鱼" '在阿里大鱼管理后台中通过审批的短信签名
Sms.TplCode = "SMS_46731512" '在阿里大鱼管理后台中通过审批的短信模板IDSMS_20185053
Sms.RecNum = Mobile ' 要接收短信的手机号码
Sms.Extend = "2356" '公共回传参数
Sms.SMSParam = Sms.ToJson(SMSParam)
'Response.Write(Sms.ToJson(SMSParam))
result = Sms.SendSMS()
If result = True Then
Response.Write("发送成功!")
Else
Response.Write(result)
End If
Set Sms = Nothing
Response.End()
End If
Function MakeRandNum()
Randomize
Dim width : width = 6 '随机数长度,默认6位
width = 10 ^ (width - 1)
MakeRandNum = Int((width*10 - width) * Rnd() + width)
End Function
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ASP_阿里大于短信好送Demo</title>
<link href="http://bootstrap.ninghao.net/assets/css/bootstrap.css" rel="stylesheet">
<link href="http://bootstrap.ninghao.net/assets/css/bootstrap-responsive.css" rel="stylesheet">
<link href="http://bootstrap.ninghao.net/assets/css/docs.css" rel="stylesheet">
<link href="http://bootstrap.ninghao.net/assets/js/google-code-prettify/prettify.css" rel="stylesheet">
<script type="text/javascript" src="http://www.w3school.com.cn/jquery/jquery-1.11.1.min.js"></script>
<style>
ul,li{ margin:0px; padding:0px; list-style-type:none}
.controls{ display:inline}
</style>
</head>
<body>
<ul class="ulist">
<li class="group">
<label class="label" for="phone">手机号:</label>
<div class="input-append">
<input class="span2" type="text" id="phone">
</div>
</li>
<li class="group">
<label class="label" for="code">验证码:</label>
<div class="input-append">
<input class="span2" type="text" id="code">
<button class="btn" type="button" id="J_getCode" onClick="getCode(this)">获取验证码</button>
</div>
</li>
</ul>
<script>
var isPhone = 1;
function getCode(e){
checkPhone(); //验证手机号码
if(isPhone){
send();
resetCode(); //倒计时
}else{
$('#phone').focus();
}
}
//验证手机号码
function checkPhone(){
var phone = $('#phone').val();
var pattern = /^1[0-9]{10}$/;
isPhone = 1;
if(phone == '') {
alert('请输入手机号码');
isPhone = 0;
return;
}
if(!pattern.test(phone)){
alert('请输入正确的手机号码');
isPhone = 0;
return;
}
}
//倒计时
function resetCode(){
$('#J_getCode').attr('disabled','disabled');
$('#J_getCode').html("<span id='J_second'>60</span>秒后重发");
$('#J_second').html('60');
var second = 60;
var timer = null;
timer = setInterval(function(){
second -= 1;
if(second >0 ){
$('#J_second').html(second);
}else{
clearInterval(timer);
$('#J_getCode').removeAttr("disabled");
$('#J_getCode').html("获取验证码");
}
},1000);
}
function send(){
var url="";
$.get(url,
{
mobile:$('#phone').val()
},
function(data,status){
alert(data);
});
}
</script>
</body>
</html>
*************Alidayu_sms_send.asp******************
<!--#include file="MD5.asp" -->
<script language="jscript" runat="server">
function getjson(str){
try{
eval("var jsonStr = (" + str + ")");
}catch(ex){
var jsonStr = null;
}
return jsonStr;
}
</script>
<%
'==================================
'=类 名 称:Alidayu_sms_send
'=功 能:阿里大于短信发送类
'=作 者:㊣FireFox㊣
'=Q Q: 63572063
'=日 期:2016-10-20
'==================================
'转载时请保留以上内容!!
Class Alidayu_sms_send
Private s_author,s_version,a_list,b_list,s_method,s_v,s_format,s_simplify,s_secret,s_app_key,s_sign_method
Private s_sms_type,s_sms_free_sign_name,s_rec_num,s_sms_template_code,s_extend,s_sms_param
Private s_error_AppKey,s_error_Secret,s_error_SignName,s_error_RecNum,s_error_TplCode
Private Sub Class_Initialize()
s_author = "FireFox"
s_version = "1.0"
s_method = "alibaba.aliqin.fc.sms.num.send" 'API接口名称,这里是短信发送的接口
s_v = "2.0" 'API协议版本
s_format = "json" '响应格式
s_simplify = false '是否采用精简JSON返回格式,仅当format=json时有效,默认值为:false
s_sign_method = "md5" '签名的摘要算法
s_sms_type = "normal" '短信类型,传入值请填写normal
s_secret = "" 'APP的Secret,加密时用
s_app_key = "" '应用的AppKey
s_sms_free_sign_name = "" '短信签名,传入的短信签名必须是在阿里大鱼“管理中心-短信签名管理”中的可用签名
s_rec_num = "" '短信接收号码。支持单个或多个手机号码,传入号码为11位手机号码,不能加0或+86。群发短信需传入多个号码,以英文逗号分隔,一次调用最多传入200个号码。示例:18600000000,13911111111,13322222222
s_sms_template_code = "" '短信模板ID,传入的模板必须是在阿里大鱼“管理中心-短信模板管理”中的可用模板
s_extend = "" '公共回传参数,在“消息返回”中会透传回该参数;举例:用户可以传入自己下级的会员ID,在消息返回时,该会员ID会包含在内,用户可以根据该会员ID识别是哪位会员使用了你的应用
s_sms_param = "" '短信模板变量,传参规则{"key":"value"},key的名字须和申请模板中的变量名一致,多个变量之间以逗号隔开。示例:针对模板“验证码${code},您正在进行${product}身份验证,打死不要告诉别人哦!”,传参时需传入{"code":"1234","product":"alidayu"}
s_error_AppKey = "AppKey属性为空。"
s_error_Secret = "Secret属性为空。"
s_error_SignName = "SignName属性为空。"
s_error_RecNum = "RecNum属性为空。"
s_error_TplCode = "TplCode属性为空。"
End Sub
Private Sub Class_Terminate()
End Sub
'应用的AppKey属性
Public Property Let AppKey(ByVal String)
s_app_key = String
End Property
'APP的Secret,加密时用
Public Property Let Secret(ByVal String)
s_secret = String
End Property
'短信签名
Public Property Let SignName(ByVal String)
s_sms_free_sign_name = String
End Property
'短信接收号码
Public Property Let RecNum(ByVal String)
s_rec_num = String
End Property
'短信模板ID
Public Property Let TplCode(ByVal String)
s_sms_template_code = String
End Property
'公共回传参数
Public Property Let Extend(ByVal String)
s_extend = String
End Property
'短信模板变量
Public Property Let SMSParam(ByVal String)
s_sms_param = String
End Property
'发送短信,成功返回True,错误返回错误信息
Public Default Function SendSMS()
If s_app_key="" Then SendSMS=s_error_AppKey:Exit Function:End If
If s_secret="" Then SendSMS=s_error_Secret:Exit Function:End If
If s_sms_free_sign_name="" Then SendSMS=s_error_SignName:Exit Function:End If
If s_rec_num="" Then SendSMS=s_error_RecNum:Exit Function:End If
If s_sms_template_code="" Then SendSMS=s_error_AppKey:Exit Function:End If
'生成参数的键数组,原始字典对象
Dim a,b,d,k,u,l,j,Arr
Set d = Server.CreateObject("Scripting.Dictionary")
'公用参数
d.RemoveAll
d.Add "method",s_method
d.Add "app_key",s_app_key
d.Add "timestamp",GetDateTimeFormat()
d.Add "format",s_format
d.Add "v",s_v
d.Add "sms_type",s_sms_type
d.Add "simplify",s_simplify
d.Add "sign_method",s_sign_method
'接口参数
d.Add "sms_free_sign_name",s_sms_free_sign_name
d.Add "rec_num",s_rec_num
d.Add "sms_template_code",s_sms_template_code
d.Add "extend",s_extend
d.Add "sms_param",s_sms_param
Arr= SortPara(MakeArray(d))
u = CreateLinkstring(Arr)
i = CreateLinkStringUrlEncode(Arr)
i = i&Md5Sign(s_secret,u)
Set d = Nothing
Dim url : url="http://gw.api.taobao.com/router/rest?"&i
j=RequestUrl(url)
'Response.Write(j)
If Instr(j,"alibaba_aliqin_fc_sms_num_send_response")>0 Then
SendSMS = True
Else
set obj = getjson(j)
if isobject(obj) Then
ret = obj.error_response.code
msg = obj.error_response.msg
If Instr(j,"sub_msg")>0 Then
sub_code = obj.error_response.sub_code
sub_msg = obj.error_response.sub_msg
End If
End If
set obj = Nothing
If sub_code="" Then
SendSMS = msg
Else
SendSMS = sub_msg
End If
End If
End Function
'Get方法请求url,获取请求内容
Public Function RequestUrl(url)
Set XmlObj = Server.CreateObject("Microsoft.XMLHTTP")'MSXML2.XMLHTTP,Microsoft.XMLHTTP
XmlObj.open "Get",url, False
XmlObj.send
RequestUrl = XmlObj.ResponseText
Set XmlObj = nothing
End Function
''
' 对数组排序
' param sPara 排序前的数组
' return 排序后的数组
Function SortPara(sPara)
Dim nCount
nCount = ubound(sPara)
For i = nCount To 0 Step -1
minmax = sPara( 0 )
minmaxSlot = 0
For j = 1 To i
mark = (sPara( j ) > minmax)
If mark Then
minmax = sPara( j )
minmaxSlot = j
End If
Next
If minmaxSlot <> i Then
temp = sPara( minmaxSlot )
sPara( minmaxSlot ) = sPara( i )
sPara( i ) = temp
End If
Next
SortPara = sPara
end function
Private Function MakeArray(d)
Dim Str
Dim k
For Each k In d
Str = Str & k & "=" & d(k) & "&"
Next
Str = Left(Str, Len(Str)-1)
MakeArray = split(Str,"&")
End Function
' 获取当前时间
' 格式:年[4位]-月[2位]-日[2位] 小时[2位 24小时制]:分[2位]:秒[2位],如:2007-10-01 13:13:13
' return 时间格式化结果
Function GetDateTimeFormat()
sTime=now()
sResult = year(sTime)&"-"&right("0" & month(sTime),2)&"-"&right("0" & day(sTime),2)&" "&right("0" & hour(sTime),2)&":"&right("0" & minute(sTime),2)&":"&right("0" & second(sTime),2)
GetDateTimeFormat = sResult
End Function
''
' 过滤特殊字符
' param Str 要被过滤的字符串
' return 已被过滤掉的新字符串
Function DelStr(Str)
If IsNull(Str) Or IsEmpty(Str) Then
Str = ""
End If
DelStr = Replace(Str,";","")
DelStr = Replace(DelStr,"'","")
DelStr = Replace(DelStr,"&","")
DelStr = Replace(DelStr," ","")
DelStr = Replace(DelStr," ","")
DelStr = Replace(DelStr,"%20","")
DelStr = Replace(DelStr,"--","")
DelStr = Replace(DelStr,"==","")
DelStr = Replace(DelStr,"<","")
DelStr = Replace(DelStr,">","")
DelStr = Replace(DelStr,"%","")
End Function
''
' MD5签名
' param prestr 需要签名的字符串
' return 签名结果
Function Md5Sign(s_secret,str)
Dim mysign
mysign = UCase(MD5((s_secret&str&s_secret),"utf-8"))
Md5Sign = "sign="&mysign
End Function
Public Function ToJson(sPara)
Dim Json_Str,key,val,i
Json_Str = "{"
For i = 0 To Ubound(sPara)
'把sPara的数组里的元素格式:变量名=值,分割开来
pos = Instr(sPara(i),"=") '获得=字符的位置
nLen = Len(sPara(i)) '获得字符串长度
key = left(sPara(i),pos-1) '获得变量名
val = right(sPara(i),nLen-pos)'获得变量的值
If i = Ubound(sPara) then
Json_Str = Json_Str & """"&key&""":"""&val&""""
Else
Json_Str = Json_Str & """"&key&""":"""&val&""","
End If
Next
Json_Str = Json_Str &"}"
ToJson = Json_Str
End Function
''
' 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
' param sPara 需要拼接的数组
' return 拼接完成以后的字符串
Function CreateLinkstring(sPara)
nCount = ubound(sPara)
dim prestr
for i = 0 to nCount
'把sPara的数组里的元素格式:变量名=值,分割开来
pos = Instr(sPara(i),"=") '获得=字符的位置
nLen = Len(sPara(i)) '获得字符串长度
itemName = left(sPara(i),pos-1) '获得变量名
itemValue = right(sPara(i),nLen-pos)'获得变量的值
prestr = prestr & itemName & itemValue
next
CreateLinkstring = prestr
End Function
''
' 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串,并且做urlencode编码
' param sPara 需要拼接的数组
' return 拼接完成以后的字符串
function CreateLinkStringUrlEncode(sPara)
nCount = ubound(sPara)
dim prestr
for i = 0 to nCount
'把sPara的数组里的元素格式:变量名=值,分割开来
pos = Instr(sPara(i),"=") '获得=字符的位置
nLen = Len(sPara(i)) '获得字符串长度
itemName = left(sPara(i),pos-1) '获得变量名
itemValue = right(sPara(i),nLen-pos)'获得变量的值
prestr = prestr & itemName &"=" & server.URLEncode(itemValue) & "&"
next
CreateLinkStringUrlEncode = prestr
end function
End Class
%>
转载:http://bbs.muyunet.com/forum.php?mod=viewthread&tid=86