SMSSDK验证码服务端校验接口

  1. 云栖社区>
  2. 博客>
  3. 正文

SMSSDK验证码服务端校验接口

皮皮酱ye 2018-09-26 15:59:13 浏览956
展开阅读全文

一、前言
有些时候开发者需要验证服务托管自己的服务器(开发者服务器拥有校验验证码的功能),然后可以更好处理自己的业务逻辑。因此,Mob平台提供了此接口实现上述需求,供开发者使用。
此接口支持
SMSSDK2.0.0以上版本(包括2.0.0)。
二、摘要
本接口属于SMSSDK短信平台的开放验证服务,不提供短信发送服务,主要是承担验证APP 使用SMSSDK发送的短信验证码,使用该接口来验证验证码是否正常。
简单梳理了一下流程,在没有短信服务端验证接口的时候,我的服务器需要知道用户是否验证成功是这样走的
1
引入了服务端验证接口我们可以这样走,本接口开放了第五步动作2
三、接口使用
第一步:开通服务端验证开关
免费短信验证码SDK/应用管理/短信设置 , 把服务端验证接口开关开启
3
第二步:请求接口验证短信
请求地址为:https://webapi.sms.mob.com/sms/verify
请求方式: POST
请求参数
4
返回结果

{status:200}

测试脚本

curl -d 'appkey=xxxx&phone=132****8362&zone=86&code=xxxx'  'https://webapi.sms.mob.com/sms/verify'

四、样例代码

phper 请看

<?php
 
// 配置项
$api = '接口地址(例:https://webapi.sms.mob.com);
$appkey = '您的appkey';
 
// 发送验证码
$response = postRequest( $api . '/sms/verify', array(
    'appkey' => $appkey,
    'phone' => '152xxxx4345',
    'zone' => '86',
    'code' => '1234',
) );
 
/**
 * 发起一个post请求到指定接口
 * 
 * @param string $api 请求的接口
 * @param array $params post参数
 * @param int $timeout 超时时间
 * @return string 请求结果
 */
function postRequest( $api, array $params = array(), $timeout = 30 ) {
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $api );
    // 以返回的形式接收信息
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
    // 设置为POST方式
    curl_setopt( $ch, CURLOPT_POST, 1 );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $params ) );
    // 不验证https证书
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
    curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
        'Accept: application/json',
    ) ); 
    // 发送数据
    $response = curl_exec( $ch );
    // 不要忘记释放资源
    curl_close( $ch );
    return $response;
}

五、C#开发者请看
感谢@金雷 开发者提供

     public static void ConnectSSL()
        {
 
            WebRequest request = WebRequest.Create("https://webapi.sms.mob.com/sms/verify");
            request.Proxy = null;
            request.Credentials = CredentialCache.DefaultCredentials;
 
            //allows for validation of SSL certificates 
 
            ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate);
            byte[] bs = Encoding.UTF8.GetBytes("appkey=xxxxxxxx&phone=xxxxxxxxxx&zone=xx&code=xxxx");
            request.Method = "Post";
            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(bs, 0, bs.Length);
            } 
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();
 
        }
 
        //for testing purpose only, accept any dodgy certificate... 
        public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            return true;
        }

六、JAVA 开发者请看
样例程序: https://github.com/tian-github/Mob_SmsSpi

    public static void main(String[] args) throws Exception {
 
        String result = requestData("https://webapi.sms.mob.com/sms/verify",
 "appkey=xxxx&phone=xxx&zone=xx&&code=xx");
        System.out.println(result);
    }
 
    /**
     * 发起https 请求
     * @param address
     * @param m
     * @return
     */
    public  static String requestData(String address ,String params){
 
            HttpURLConnection conn = null;
            try {
            // Create a trust manager that does not validate certificate chains
            TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager(){
                public X509Certificate[] getAcceptedIssuers(){return null;}
                public void checkClientTrusted(X509Certificate[] certs, String authType){}
                public void checkServerTrusted(X509Certificate[] certs, String authType){}
            }};
 
            // Install the all-trusting trust manager
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, trustAllCerts, new SecureRandom());
 
            //ip host verify
            HostnameVerifier hv = new HostnameVerifier() {
                 public boolean verify(String urlHostName, SSLSession session) {
                 return urlHostName.equals(session.getPeerHost());
                 }
            };
 
            //set ip host verify
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
 
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
 
            URL url = new URL(address);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");// POST
            conn.setConnectTimeout(3000);
            conn.setReadTimeout(3000);
            // set params ;post params 
            if (params!=null) {
                conn.setDoOutput(true);
                DataOutputStream out = new DataOutputStream(conn.getOutputStream());
                out.write(params.getBytes(Charset.forName("UTF-8")));
                out.flush();
                out.close();
            }
            conn.connect();
            //get result 
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                String result = parsRtn(conn.getInputStream());
                return result;
            } else {
                System.out.println(conn.getResponseCode() + " "+ conn.getResponseMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null)
                conn.disconnect();
        }
        return null;
    }

七、python 开发者请看
感谢@jimidata 开发者提供,谢谢支持
样例程序: https://github.com/freeznet/py_mob_sms

#!/usr/bin/env python
# encoding: utf-8
import requests
 
__author__ = 'rui'
 
class MobSMS:
    def __init__(self, appkey):
        self.appkey = appkey
        self.verify_url = 'https://webapi.sms.mob.com/sms/verify'
 
    def verify_sms_code(self, zone, phone, code, debug=False):
        if debug:
            return 200
 
        data = {'appkey': self.appkey, 'phone': phone, 'zone': zone, 'code': code}
        req = requests.post(self.verify_url, data=data, verify=False)
        if req.status_code == 200:
            j = req.json()
            return j.get('status', 500)
 
        return 500
 
if __name__ == '__main__':
    mobsms = MobSMS('your_mob_sms_key_goes_here')
    print mobsms.verify_sms_code(86, 13900000000, '1234')

网友评论

登录后评论
0/500
评论
皮皮酱ye
+ 关注