NODE.JS怎么给手机发短信对接验证码短信接口DEMO示例

发现一个超简单验证码短信接口对接DEMO示例

步骤

1.注册 互亿无线 账号

2.查找APIID和APIKEY

3.代码的书写

注册 互亿无线 账号

网站: http://user.ihuyi.com/?JA9Ewr

查找APIID和APIKEY

注意事项:

(1)调试期间,请使用用系统默认的短信内容:您的验证码是:【变量】。请不要把验证码泄露给其他人。

/**

* Created by XadillaX on 14-2-12.

* https://github.com/XadillaX/ihuyi106js

*/

String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) {

    if (!RegExp.prototype.isPrototypeOf(reallyDo)) {

        return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi": "g")), replaceWith);

    } else {

        return this.replace(reallyDo, replaceWith);

    }

};

var dom = require('xmldom').DOMParser;

var _baseUri = "http://106.ihuyi.com/webservice/sms.php?method=Submit";

var _userAgent = "node-ihuyi106-module by 死月 (admin@xcoder.in)";

/**

* iHuyi constructure.

* @param account

* @param password 查看密码请登录用户中心->验证码、通知短信->帐户及签名设置->APIKEY

*/

var iHuyi = function(account, password) {

    this.spidex = require("spidex");

    this.spidex.setDefaultUserAgent(_userAgent);

    this.account = account;

    this.password = password;

};

/**

* send an SMS.

* @param mobile

* @param content

* @param callback

*/

iHuyi.prototype.send = function(mobile, content, callback) {

    var data = {

        account        : this.account,

        password        : this.password,

        mobile          : mobile,

        content        : content

    };

    this.spidex.post(_baseUri, function(html, status) {

        if(status !== 200) {

            callback(new Error("短信发送服务器响应失败。"));

            return;

        }

        html = html.replaceAll("\r", "");

        html = html.replaceAll("\n", "");

        html = html.replaceAll(" xmlns=\"http://106.ihuyi.com/\"", "");

        //console.log(html);

        var doc = new dom().parseFromString(html);

        var result = doc.lastChild;

        var json = {};

        for(var node = result.firstChild; node !== null; node = node.nextSibling) {

            json[node.tagName] = node.firstChild.data;

        }

        //console.log(json);

        if(json.code == "2") {

            callback(null, json.smsid);

        } else {

            callback(new Error(json.msg, parseInt(json.code)));

        }

    }, data, "utf8").on("err", function(e) {

        callback(e);

    });

};

module.exports = iHuyi;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值