你在这里: 代码示例

代码示例

var https = require('https');
var qs = require('querystring');

//修改为您的apikey. apikey可在官网(https://www.dingdongcloud.com)登录后获取
var apikey = '***************';
// 修改为您要发送的手机号码
var mobile = '***************';

//验证码内容
var contentyzm ="【叮咚云】您的验证码是:1234"

var tplId = 1

var tplValue= {'验证码':'1234'};

// 获取user信息url
var url_get_user        = "/v2/user/get";
// 发送验证码url
var url_send_single    = "/v2/sms/single_send";
// 指定模版发送url
var url_send_tpl_single = "/v2/sms/tpl_single_send";


var sms_host = 'api.dingdongcloud.com';

//获取用户信息
get_user_info(url_get_user,apikey);
//发送单条短信
send_sms(url_send_single,apikey,mobile,contentyzm);
//指定模版单条发送
send_tpl_single(url_send_tpl_single,apikey,mobile,tplId,tplValue);


function get_user_info(uri,apikey){
    var post_data = {
    'apikey': apikey,
    };//这是需要提交的数据
    var postdata = qs.stringify(post_data);
    post(uri,postdata,sms_host);
}

function send_sms(uri,apikey,mobile,content){
    var post_data = {
    'apikey': apikey,
    'mobile':mobile,
    'content':content,
    };//这是需要提交的数据
    var postdata = qs.stringify(post_data);
    post(uri,postdata,sms_host);
}

function send_tpl_single(uri,apikey,mobile,tplId,tplValue){
    var postData = {
        'apikey': apikey,
        'mobile': mobile,
        'tplId': tplId,
        'tplValue': qs.stringify(tplValue)
    }

    var postdata = qs.stringify(post_data);
    post(uri,postdata,sms_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',
            'Content-Length' : content.length
        }  
    };
    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();
}

售前QQ:957316019联系售前

13208047187

常见售前问题:

可以发哪些类型短信?
叮咚云短信的价格?
如何接入叮咚云验证码/通知短信?

》查看更多常见问题《