多语言展示
当前在线:1302今日阅读:161今日分享:11

创蓝短信验证码接口相关内容

短信验证码在各类App和网站中广泛使用,这里介绍一些技术对接中常用的接口以及相关实例为各位提供参考。首先,预先提供短信模板,保证通道提供稳定服务,平台进行内容审核,而后方能使用通用发送接口发短信,该接口能够自动匹配已经审核通过的模板进行发送。调用模板发送接口时需要用POST或GET方式提交各类参数,如:tpl_id、tpl_value。在创蓝技术平台开发的接口包括:ASP,ASP.NET(C#),C(C++),GO,JAVA,PHP,PYTHON等,不同的接口语音中,在任意应用场景中,各类模板的使用方法基本类似,设置对应的模板id(tpl_id)和相关的变量值(tpl_value)即可。以下专门挑出两大常见短信验证码接口语言进行实例说明:
方法/步骤
1

一、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();}}}

2

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 = ',';// 手机号码,多个号码使用','分割String msg = '亲爱的用户,您的验证码是123456,5分钟内有效。';// 短信内容boolean needstatus = true;// 是否需要状态报告,需要true,不需要falseString product = null;// 产品IDString 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();}}}

3

二、PHP语言文档a) 账户配置信息

4

b) 短信接口 $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;}}?>

5

c) 平台示例sendSMS('', '您好,您的验证码是888888','true');$result = $clapi->execResult($result);if($result[1]==0){echo '发送成功';}else{echo '发送失败{$result[1]}';}var_dump($result);

6

任何短信接口都是基于其相应语言开发的协议,所有用同种语言开发的程序都可以调用这个接口发送短信,通过其短信接口传递参数至创蓝短信的服务器,最终完成下发。其实短信接口测试之前,各位专业人士都明白,影响短信验证码速度和到达率的因素包含三个方面:

7

第一,机房的网络构架。IDG机房的质量参差不齐,从创蓝以往选择机房的经验来说,上海最好机房是目前我们所在的南汇机房。验证码是个持续不断的长期技术活,服务器的运转1分钟都不能断,如若服务商采用五星机房,配合BGP网络,自动识别用户提交短信的网络,再将移动号码分包到移动机房,再提交到移动网关,电信号码分包到电信机房,再提交到电信网关,这就解决了短信timeout的问题,保证验证码的到达率。同时,在多个重点城市进行中心机房布点,备份两条以上的链路,集群模式服务,防止任何时间的中断,保证验证码在任何一分钟都不会因为网络而出现延迟或者收不到。

8

第二,平台的发送机制的问题。若大家都是做App项目的,关于如何解决大批量延迟问题,都很好理解,对于事实发送创蓝采用:一,内存数据库技术,HASH散列存储,并采用二级索引进行快速排序,查找;二,在实时处理的信息时,按照大数概率和正态分布控制实时内存窗口,提高实时短信发送效率;第三,对于低优先级的数据,按照权重进行发送流速均衡分配;第四,短信流控采用令牌获取机制,做到原子操作,实现流速精确均衡控制。

10

最后,由于端口的码号越短,质量越高,给用户使用的都是11位短码,也就是一个手机号的长度。降低了360等软件的拦截概率,也避免了各省运营商之间的相互屏蔽机制。从而在通道层面保证了短信的速度和成功率。

推荐信息