乐趣区

关于rest-api:MobTech-短信验证REST-API

前言

本接口属于 SMSSDK 短信平台的凋谢验证服务, 不提供短信发送服务, 次要是承当验证 APP 应用 SMSSDK 发送的短信验证码,应用该接口来验证验证码是否失常。

接口应用

开明服务端验证开关
第一步:开明服务端验证开关 在开发者后盾创立利用后开启 SMSSDK,并配置服务器白名单

申请接口验证短信
申请地址为:https://webapi.sms.mob.com/sm…

申请形式: POST

申请参数

返回后果 {status:200}

测试脚本

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

样例代码

注:仅供参考

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 开发者请看

 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 开发者请看

#!/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')
退出移动版