MobTech 短信验证 REST API
作者:MobTech袤博科技
- 2022 年 9 月 01 日 上海
本文字数:3215 字
阅读完需:约 11 分钟
前言
本接口属于 SMSSDK 短信平台的开放验证服务,不提供短信发送服务,主要是承担验证 APP 使用 SMSSDK 发送的短信验证码,使用该接口来验证验证码是否正常。
接口使用
开通服务端验证开关第一步:开通服务端验证开关 在开发者后台创建应用后开启 SMSSDK,并配置服务器白名单
请求接口验证短信请求地址为:https://webapi.sms.mob.com/sms/verify
请求方式: 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')
复制代码
划线
评论
复制
发布于: 刚刚阅读数: 4
MobTech袤博科技
关注
还未添加个人签名 2019.05.08 加入
还未添加个人简介
评论