Nodejs實現(xiàn)短信驗證碼功能
更新時間:2017年02月09日 10:36:38 作者:睡不夠大師
使用Nodejs的開發(fā)者愈來越多,基于Nodejs的后臺開發(fā)也多了起來,像短信驗證碼、短信群發(fā)、國際短信這些需求,完全可以采用第三方接口來實現(xiàn),云片就提供了這樣的接口
使用Nodejs的開發(fā)者愈來越多,基于Nodejs的后臺開發(fā)也多了起來,像短信驗證碼、短信群發(fā)、國際短信這些需求,完全可以采用第三方接口來實現(xiàn),云片就提供了這樣的接口。
Nodejs
// 修改為您的apikey.可在官網(wǎng)(https://www.yunpian.com)登錄后獲取 var https = require('https'); var qs = require('querystring'); var apikey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // 修改為您要發(fā)送的手機號碼,多個號碼用逗號隔開 var mobile = 'xxxxxxxxxxx'; // 修改為您要發(fā)送的短信內容 var text = '【云片網(wǎng)】您的驗證碼是1234'; // 指定發(fā)送的模板編號 var tpl_id = 1; // 指定發(fā)送模板的內容 var tpl_value = {'#code#':'1234','#company#':'yunpian'}; // 語音短信的內容 var code = '1234'; // 查詢賬戶信息https地址 var get_user_info_uri = '/v2/user/get.json'; // 智能匹配模板發(fā)送https地址 var sms_host = 'sms.yunpian.com'; var voice_host = 'voice.yunpian.com'; send_sms_uri = '/v2/sms/single_send.json'; // 指定模板發(fā)送接口https地址 send_tpl_sms_uri = '/v2/sms/tpl_single_send.json'; // 發(fā)送語音驗證碼接口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, };//這是需要提交的數(shù)據(jù) 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, };//這是需要提交的數(shù)據(jù) 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), };//這是需要提交的數(shù)據(jù) 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, };//這是需要提交的數(shù)據(jù) 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-www-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(); }
上面就是云片的全部接口,在實際使用的過程中,可以根據(jù)自己的需求,選擇對應的接口使用,具體的可以看這篇文章如何使用云片API發(fā)送短信驗證碼,里面講了如何使用單發(fā)短信API、群發(fā)短信API、不同短信內容批量發(fā)送API,很實用。
另外最重要的是,云片的服務還不錯,短信的到達率比較高,出了問題也有人及時回復,這點在國內所有SaaS廠家中,算是做得很好的一家。
相關文章
Nodejs Buffer的使用及Stream流和事件機制詳解
這篇文章主要為大家介紹了Nodejs Buffer的使用及Stream流和事件機制詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10node故障定位頂級技巧動態(tài)追蹤Dynamic?Trace詳解
這篇文章主要為大家介紹了node故障定位頂級技巧動態(tài)追蹤Dynamic?Trace詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09