Nodejs实现短信验证码功能
网络编程 2021-07-04 19:20www.168986.cn编程入门
使用Nodejs的开发者愈来越多,基于Nodejs的后台开发也多了起来,像短信验证码、短信群发、国际短信这些需求,完全可以采用第三方接口来实现,云片就提供了这样的接口
使用Nodejs的开发者愈来越多,基于Nodejs的后台开发也多了起来,像短信验证码、短信群发、国际短信这些需求,完全可以采用第三方接口来实现,云片就提供了这样的接口。
Nodejs
// 修改为您的apikey.可在官网(https://.yunpian.)登录后获取 var https = require('https'); var qs = require('querystring'); var apikey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // 修改为您要发送的手机号码,多个号码用逗号隔开 var mobile = 'xxxxxxxxxxx'; // 修改为您要发送的短信内容 var text = '【云片网】您的验证码是1234'; // 指定发送的模板编号 var tpl_id = 1; // 指定发送模板的内容 var tpl_value = {'#code#':'1234','#pany#':'yunpian'}; // 语音短信的内容 var code = '1234'; // 查询账户信息https地址 var get_user_info_uri = '/v2/user/get.json'; // 智能匹配模板发送https地址 var sms_host = 'sms.yunpian.'; var voice_host = 'voice.yunpian.'; send_sms_uri = '/v2/sms/single_send.json'; // 指定模板发送接口https地址 send_tpl_sms_uri = '/v2/sms/tpl_single_send.json'; // 发送语音验证码接口https地址 send_voice_uri = '/v2/voice/send.json'; query_user_info(get_user_info_uri,apikey); send_sms(send_sms_uri,apikey,mobile,text); send_tpl_sms(send_tpl_sms_uri,apikey,mobile,tpl_id,tpl_value); send_voice_sms(send_voice_uri,apikey,mobile,code); function query_user_info(uri,apikey){ var post_data = { 'apikey': apikey, };//这是需要提交的数据 var content = qs.stringify(post_data); post(uri,content,sms_host); } function send_sms(uri,apikey,mobile,text){ var post_data = { 'apikey': apikey, 'mobile':mobile, 'text':text, };//这是需要提交的数据 var content = qs.stringify(post_data); post(uri,content,sms_host); } function send_tpl_sms(uri,apikey,mobile,tpl_id,tpl_value){ var post_data = { 'apikey': apikey, 'mobile':mobile, 'tpl_id':tpl_id, 'tpl_value':qs.stringify(tpl_value), };//这是需要提交的数据 var content = qs.stringify(post_data); post(uri,content,sms_host); } function send_voice_sms(uri,apikey,mobile,code){ var post_data = { 'apikey': apikey, 'mobile':mobile, 'code':code, };//这是需要提交的数据 var content = qs.stringify(post_data); console.log(content); post(uri,content,voice_host); } function post(uri,content,host){ var options = { hostname: host, port: 443, path: uri, method: 'POST', headers: { 'Content-Type': 'application/x--form-urlencoded; charset=UTF-8' } }; var req = https.request(options, function (res) { // console.log('STATUS: ' + res.statusCode); // console.log('HEADERS: ' + JSON.stringify(res.headers)); res.setEncoding('utf8'); res.on('data', function (chunk) { console.log('BODY: ' + chunk); }); }); //console.log(content); req.write(content); req.end(); }
上面就是云片的全部接口,在实际使用的过程中,可以根据自己的需求,选择对应的接口使用,具体的可以看这篇文章,里面讲了如何使用单发短信API、群发短信API、不同短信内容批量发送API,很实用。
另外最重要的是,云片的服务还不错,短信的到达率比较高,出了问题也有人及时回复,这点在国内所有SaaS厂家中,算是做得很好的一家。
上一篇:jquery dialog获取焦点的方法
下一篇:jQuery实现淡入淡出的模态框
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程