-
短信验证码在各类App和网站中广泛使用,这里介绍一些技术对接中常用的接口以及相关实例为各位提供参考。
首先,预先提供短信模板,保证通道提供稳定服务,平台进行内容审核,而后方能使用通用发送接口发短信,该接口能够自动匹配已经审核通过的模板进行发送。调用模板发送接口时需要用POST或GET方式提交各类参数,如:tpl_id、tpl_value。在创蓝技术平台开发的接口包括:ASP,ASP.NET(C#),C(C++),GO,JAVA,PHP,PYTHON等,不同的接口语音中,在任意应用场景中,各类模板的使用方法基本类似,设置对应的模板id(tpl_id)和相关的变量值(tpl_value)即可。
以下专门挑出两大常见短信验证码接口语言进行实例说明:
方法/步骤
-
一、JAVA语言文档
a) 短信接口
package com.bcloud.msg.http;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URLDecoder;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.methods.GetMethod;
/**
* @author Beyond
*/
public class HttpSender {
/**
*
* @param url 应用地址,类似于http://ip:port/msg/
* @param account 账号
* @param pswd 密码
* @param mobile 手机号码,多个号码使用","分割
* @param msg 短信内容
* @param needstatus 是否需要状态报告,需要true,不需要false
* @return 返回值定义参见HTTP协议文档
* @throws Exception
*/
public static String send(String url, String account, String pswd, String mobile, String msg,
boolean needstatus, String product, String extno) throws Exception {
HttpClient client = new HttpClient();
GetMethod method = new GetMethod();
try {
URI base = new URI(url, false);
method.setURI(new URI(base, "HttpSendSM", false));
method.setQueryString(new NameValuePair[] {
new NameValuePair("account", account),
new NameValuePair("pswd", pswd),
new NameValuePair("mobile", mobile),
new NameValuePair("needstatus", String.valueOf(needstatus)),
new NameValuePair("msg", msg),
new NameValuePair("product", product),
new NameValuePair("extno", extno),
});
int result = client.executeMethod(method);
if (result == HttpStatus.SC_OK) {
InputStream in = method.getResponseBodyAsStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = in.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
return URLDecoder.decode(baos.toString(), "UTF-8");
} else {
throw new Exception("HTTP ERROR Status: " + method.getStatusCode() + ":" + method.getStatusText());
}
} finally {
method.releaseConnection();
}
}
/**
*
* @param url 应用地址,类似于http://ip:port/msg/
* @param account 账号
* @param pswd 密码
* @param mobile 手机号码,多个号码使用","分割
* @param msg 短信内容
* @param needstatus 是否需要状态报告,需要true,不需要false
* @return 返回值定义参见HTTP协议文档
* @throws Exception
*/
public static String batchSend(String url, String account, String pswd, String mobile, String msg,
boolean needstatus, String product, String extno) throws Exception {
HttpClient client = new HttpClient();
GetMethod method = new GetMethod();
try {
URI base = new URI(url, false);
method.setURI(new URI(base, "HttpBatchSendSM", false));
method.setQueryString(new NameValuePair[] {
new NameValuePair("account", account),
new NameValuePair("pswd", pswd),
new NameValuePair("mobile", mobile),
new NameValuePair("needstatus", String.valueOf(needstatus)),
new NameValuePair("msg", msg),
new NameValuePair("product", product),
new NameValuePair("extno", extno),
});
int result = client.executeMethod(method);
if (result == HttpStatus.SC_OK) {
InputStream in = method.getResponseBodyAsStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = in.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
return URLDecoder.decode(baos.toString(), "UTF-8");
} else {
throw new Exception("HTTP ERROR Status: " + method.getStatusCode() + ":" + method.getStatusText());
}
} finally {
method.releaseConnection();
}
}
}
-
b) 后台示例
import com.bcloud.msg.http.HttpSender;
public class HttpSenderTest {
public static void main(String[] args) {
String url = "http://222.73.117.158/msg/";// 应用地址
String account = "询问对接人";// 账号
String pswd = "询问对接人";// 密码
String mobile = "13800210021,13800138000";// 手机号码,多个号码使用","分割
String msg = "亲爱的用户,您的验证码是123456,5分钟内有效。";// 短信内容
boolean needstatus = true;// 是否需要状态报告,需要true,不需要false
String product = null;// 产品ID
String extno = null;// 扩展码
try {
String returnString = HttpSender.batchSend(url, account, pswd, mobile, msg, needstatus, product, extno);
System.out.println(returnString);
// TODO 处理返回值,参见HTTP协议文档
} catch (Exception e) {
// TODO 处理异常
e.printStackTrace();
}
}
}
-
二、PHP语言文档
a) 账户配置信息
<?php
/* *
* 配置文件
* 版本:1.2
* 日期:2014-07-16
* 说明:
* 以下代码只是为了方便客户测试而提供的样例代码,客户可以根据自己网站的需要自行编写,并非一定要使用该代码。
* 该代码仅供学习和研究接口使用,只是提供一个参考。
*/
//↓↓↓↓↓↓↓↓↓↓请在这里配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
//创蓝发送短信接口URL, 如无必要,该参数可不用修改
$chuanglan_config['api_send_url'] = 'http://222.73.117.158/msg/HttpBatchSendSM';
//创蓝短信余额查询接口URL, 如无必要,该参数可不用修改
$chuanglan_config['api_balance_query_url'] = 'http://222.73.117.158/msg/QueryBalance';
//创蓝账号 替换成你自己的账号
$chuanglan_config['api_account'] = 'jiekou-clcs-07';
//创蓝密码 替换成你自己的密码
$chuanglan_config['api_password'] = 'Clwh2009';
//↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
?>
-
b) 短信接口
<?php
/* *
* 类名:ChuanglanSmsApi
* 功能:创蓝接口请求类
* 详细:构造创蓝短信接口请求,获取远程HTTP数据
* 版本:1.3
* 日期:2014-07-16
* 说明:
* 以下代码只是为了方便客户测试而提供的样例代码,客户可以根据自己网站的需要,按照技术文档自行编写,并非一定要使用该代码。
* 该代码仅供学习和研究创蓝接口使用,只是提供一个参考。
*/
require_once("chuanglan_config.php");
class ChuanglanSmsApi {
/**
* 发送短信
*
* @param string $mobile 手机号码
* @param string $msg 短信内容
* @param string $needstatus 是否需要状态报告
* @param string $product 产品id,可选
* @param string $extno 扩展码,可选
*/
public function sendSMS( $mobile, $msg, $needstatus = 'false', $product = '', $extno = '') {
global $chuanglan_config;
//创蓝接口参数
$postArr = array (
'account' => $chuanglan_config['api_account'],
'pswd' => $chuanglan_config['api_password'],
'msg' => $msg,
'mobile' => $mobile,
'needstatus' => $needstatus,
'product' => $product,
'extno' => $extno
);
$result = $this->curlPost( $chuanglan_config['api_send_url'] , $postArr);
return $result;
}
/**
* 查询额度
*
* 查询地址
*/
public function queryBalance() {
global $chuanglan_config;
//查询参数
$postArr = array (
'account' => $chuanglan_config['api_account'],
'pswd' => $chuanglan_config['api_password'],
);
$result = $this->curlPost($chuanglan_config['api_balance_query_url'], $postArr);
return $result;
}
/**
* 处理返回值
*
*/
public function execResult($result){
$result=preg_split("/[,\r\n]/",$result);
return $result;
}
/**
* 通过CURL发送HTTP请求
* @param string $url //请求URL
* @param array $postFields //请求参数
* @return mixed
*/
private function curlPost($url,$postFields){
$postFields = http_build_query($postFields);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postFields );
$result = curl_exec ( $ch );
curl_close ( $ch );
return $result;
}
//魔术获取
public function __get($name){
return $this->$name;
}
//魔术设置
public function __set($name,$value){
$this->$name=$value;
}
}
?>
-
c) 平台示例
<?php
/* *
* 功能:创蓝发送信息DEMO
* 版本:1.3
* 日期:2014-07-16
* 说明:
* 以下代码只是为了方便客户测试而提供的样例代码,客户可以根据自己网站的需要,按照技术文档自行编写,并非一定要使用该代码。
* 该代码仅供学习和研究创蓝接口使用,只是提供一个参考。
*/
require_once 'ChuanglanSmsHelper/ChuanglanSmsApi.php';
$clapi = new ChuanglanSmsApi();
$result = $clapi->sendSMS('18516590414', '您好,您的验证码是888888','true');
$result = $clapi->execResult($result);
if($result[1]==0){
echo '发送成功';
}else{
echo "发送失败{$result[1]}";
}
var_dump($result);
-
任何短信接口都是基于其相应语言开发的协议,所有用同种语言开发的程序都可以调用这个接口发送短信,通过其短信接口传递参数至创蓝短信的服务器,最终完成下发。其实短信接口测试之前,各位专业人士都明白,影响短信验证码速度和到达率的因素包含三个方面:
-
第一,机房的网络构架。IDG机房的质量参差不齐,从创蓝以往选择机房的经验来说,上海最好机房是目前我们所在的南汇机房。验证码是个持续不断的长期技术活,服务器的运转1分钟都不能断,如若服务商采用五星机房,配合BGP网络,自动识别用户提交短信的网络,再将移动号码分包到移动机房,再提交到移动网关,电信号码分包到电信机房,再提交到电信网关,这就解决了短信timeout的问题,保证验证码的到达率。同时,在多个重点城市进行中心机房布点,备份两条以上的链路,集群模式服务,防止任何时间的中断,保证验证码在任何一分钟都不会因为网络而出现延迟或者收不到。
-
第二,平台的发送机制的问题。若大家都是做App项目的,关于如何解决大批量延迟问题,都很好理解,对于事实发送创蓝采用:一,内存数据库技术,HASH散列存储,并采用二级索引进行快速排序,查找;二,在实时处理的信息时,按照大数概率和正态分布控制实时内存窗口,提高实时短信发送效率;第三,对于低优先级的数据,按照权重进行发送流速均衡分配;第四,短信流控采用令牌获取机制,做到原子操作,实现流速精确均衡控制。
-
第三,通道的带宽问题。10690时代,短信的通道资源非常珍贵。一家企业,注册资金1000万,只能申请1跟通道,并且从申请到能用的周期很长,超过2年。一根通道的流速上限是1000,也就是说一根通道每小时的发送量是360万,创蓝在单根通道上面的使用率控制在1/30,也就是一个通道,每个小时走的短信不会超过12万条,这样放的量越小,通道的压力越小。同时,为了保证验证码不断流,对每一位合作用户都采取双通道备份。这样以来,大大的提高了验证码的稳定性。
-
最后,由于端口的码号越短,质量越高,给用户使用的都是11位短码,也就是一个手机号的长度。降低了360等软件的拦截概率,也避免了各省运营商之间的相互屏蔽机制。从而在通道层面保证了短信的速度和成功率。
-
-
短信接口示例
最新推荐文章于 2024-08-13 15:49:43 发布