一、本文目的
- 大部分平台都有一个接入发送短信验证码、通知短信的需求。虽然市场上大部分平台的接口都只是一个非常普通的HTTP-GET请求,但终归有需要学习和借鉴使用的朋友。
- 本文的初衷是主要提供学习便利,方便初学者学习简单的http接口对接实现,由于各大短信通道服务商API参数基本相同,本Demo可以适当减少开发者对接短信通道接口的开发成本,提供参考。
- 若有朋友不想从文章中复制源码,需要直接下载,源码已上传至GitHub。
- GitHub传送门:https://github.com/yzchen0o0/demo-sms
二、发送短信接口请求
- 请求参数模板
https://{{url}}?appKey={{app_key}}&appSecret={{app_secret}}&phones={{mobile}}&content=【{{sign}}】{{content}}
- 示例
https://xxxyun.com/sendsms?appKey=aaaaa&appSecret=bbbb&phones=13888888888&content=【某云】您的验证码是:666666
三、参数描述
参数名 | 说明 |
---|---|
url | 请求地址 |
app_key | 客户在供应商注册的唯一key码 |
app_secret | 客户在供应商注册的唯一secret码 |
mobile | 接收短信的用户手机号 |
sign | 短信内容的产品名称 |
四、各开发语言实现
1、Java
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* 短信发送工具类
* <p>
* http接口请求依赖 okhttp-3.14.2.jar, okio-1.17.2.jar
*</p>
*/
public class SmsUtil {
private static final String HOST = "https://api.zhuanxinyun.com/api/v2/sendSms.json";
private static final String SIGN = "【签名】";
private static final String APP_KEY = "app_key";
private static final String APP_SECRET = "app_secret";
/**
* 测试请求
* @param args
*/
public static void main(String[] args) {
String mobile = "18566770000";
String code = "666666";
String body = sendSmsCode(mobile, code);
System.out.println(body);
}
/**
* 发送短信验证码
* @param mobile 接收手机号
* @param code 验证码
*/
public static String sendSmsCode(String mobile, String code) {
StringBuffer content = new StringBuffer().append("验证码:" ).append(code).append(",如非本人操作请忽略。");
return sendSms(mobile, content.toString());
}
/**
* 发送短信信息
* @param mobile 接收手机号
* @param content 短信内容
*/
public static String sendSms(String mobile, String content) {
// 拼接请求参数
StringBuffer url = new StringBuffer().append(HOST).append("?appKey=").append(APP_KEY).append("&appSecret=")
.append(APP_SECRET).append("&phones=").append(mobile).append("&content=").append(SIGN).append(content);
// 封装请求参数
Request request = new Request.Builder().url(url.toString()).get().build();
OkHttpClient client = new OkHttpClient();
try {
// 发起请求
Response response = client.newCall(request).execute();
return response.body().string();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
2、php
<?php
$host = "https://api.zhuanxinyun.com/api/v2/sendSms.json";
$sign = "【签名】";
$app_key = "app_key";
$app_secret = "app_secret";
$code = "6666";
$phones = "18088888888";
$content = "本次验证码是".code.",如非本人操作请忽略。";
$uri = $host."?appKey=".$app_key."&appSecret=".$app_secret."&phones=".$phones."&content=".$sign.$content;
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
curl_close($ch);
var_dump($contents);//输入返回内容
?>
3、.Net
using System;
using System.IO;
using System.Net;
using System.Text;
namespace SmsCode
{
class Program
{
private static String HOST = "https://api.zhuanxinyun.com/api/v2/sendSms.json";
private static String SIGN = "【签名】";
private static String APP_KEY = "app_key";
private static String APP_SECRET = "APP_SECRET";
static void Main(string[] args)
{
string mobile = "手机号";
string code = "内容";
string body = sendSmsCode(mobile, code);
Console.WriteLine(body);
}
/**
* 发送短信验证码
* @param mobile 接收手机号
* @param code 验证码
*/
public static String sendSmsCode(String mobile, String code)
{
StringBuilder content = new StringBuilder().Append("验证码:").Append(code).Append(",如非本人操作请忽略。");
return sendSms(mobile, content.ToString());
// StringBuilder
}
/**
* 发送短信信息
* @param mobile 接收手机号
* @param content 短信内容
*/
public static string sendSms(String mobile, String content)
{
string msg = string.Empty;
// 拼接请求参数
StringBuilder url = new StringBuilder().Append(HOST).Append("?appKey=").Append(APP_KEY).Append("&appSecret=")
.Append(APP_SECRET).Append("&phones=").Append(mobile).Append("&content=").Append(SIGN).Append(content);
// 封装请求参数
try
{
// 发起请求
msg = Request_GET(url.ToString(), "UTF-8");
}
catch (Exception ex)
{
msg=ex.Message;
}
return msg;
}
public static string Request_GET(string urlString, string encoding)
{
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null;
Stream stream = null;
StreamReader streamReader = null;
string result = string.Empty;
try
{
httpWebRequest = (WebRequest.Create(urlString) as HttpWebRequest);
httpWebRequest.Method = "GET";
//httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; Maxthon 2.0)";
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
stream = httpWebResponse.GetResponseStream();
streamReader = new StreamReader(stream, Encoding.GetEncoding(encoding));
result = streamReader.ReadToEnd();
}
catch (SystemException ex)
{
result = "err:" + ex.Message;
}
finally
{
if (httpWebRequest != null)
{
httpWebRequest.Abort();
}
if (httpWebResponse != null)
{
httpWebResponse.Close();
}
if (stream != null)
{
stream.Close();
}
if (streamReader != null)
{
streamReader.Close();
}
}
return result;
}
}
}
五、返回值
{"errorCode":"000000","errorMsg":"提交成功"}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml>
<errorCode>000000</errorCode>
<errorMsg>提交成功</errorMsg>
</xml>
技术交流
My Blog
blog.guijianpan.com
原创文章,作者:carmelaweatherly,如若转载,请注明出处:https://blog.ytso.com/184894.html