First, the purpose of this paper
- Most platforms have an access code to send text messages, SMS notification requirements. Although most of the interface platform on the market are just a very ordinary HTTP-GET request, but after all, there is a need to learn from friends to use.
- The main intention of this article is to provide learning convenient, easy for beginners to learn simple http interface, docking to achieve, due to the major service providers SMS channel API is basically the same parameters, the Demo can be appropriate to reduce development costs developers docking SMS channel interface, to provide a reference.
- If friends do not want to copy the source code from the article, you need to direct download, source code has been uploaded to GitHub.
- GitHub Portal : https://github.com/yzchen0o0/demo-sms
Second, the interface transmits a request message
- Request parameter template
https://{{url}}?appKey={{app_key}}&appSecret={{app_secret}}&phones={{mobile}}&content=【{{sign}}】{{content}}
- Examples
https://xxxyun.com/sendsms?appKey=aaaaa&appSecret=bbbb&phones=13888888888&content=【某云】您的验证码是:666666
Third, Parameter Description
parameter name |
Explanation |
url |
Request address |
app_key |
Customers suppliers registered unique key code |
app_secret |
Customers suppliers registered unique secret code |
mobile |
The mobile phone number to receive text messages |
sign |
SMS content of the product name |
Fourth, the development of each language
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 = "18566775162";
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;
}
}
}
Fifth, the return value
{"errorCode":"000000","errorMsg":"提交成功"}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml>
<errorCode>000000</errorCode>
<errorMsg>提交成功</errorMsg>
</xml>
Technology Exchange
My Blog
blog.guijianpan.com