1、本文目的
- 大部分平臺都有一個接入發送短信驗證碼、通知短信的需求。雖然市場上大部分平臺的接口都只是一個很是普通的HTTP-GET請求,但終歸有須要學習和借鑑使用的朋友。
- 本文的初衷是主要提供學習便利,方便初學者學習簡單的http接口對接實現,因爲各大短信通道服務商API參數基本相同,本Demo能夠適當減小開發者對接短信通道接口的開發成本,提供參考。
- 如有朋友不想從文章中複製源碼,須要直接下載,源碼已上傳至GitHub。
- GitHub傳送門:github.com/yzchen0o0/d…
2、發送短信接口請求
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
複製代碼
3、參數描述
參數名 |
說明 |
url |
請求地址 |
app_key |
客戶在供應商註冊的惟一key碼 |
app_secret |
客戶在供應商註冊的惟一secret碼 |
mobile |
接收短信的用戶手機號 |
sign |
短信內容的產品名稱 |
4、各開發語言實現
一、Java
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
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";
public static void main(String[] args) {
String mobile = "18566770000";
String code = "666666";
String body = sendSmsCode(mobile, code);
System.out.println(body);
}
public static String sendSmsCode(String mobile, String code) {
StringBuffer content = new StringBuffer().append("驗證碼:" ).append(code).append(",如非本人操做請忽略。");
return sendSms(mobile, content.toString());
}
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;
}
}
複製代碼
二、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);
?>
複製代碼
三、.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;
}
}
}
複製代碼
5、返回值
{"errorCode":"000000","errorMsg":"提交成功"}
複製代碼
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml>
<errorCode>000000</errorCode>
<errorMsg>提交成功</errorMsg>
</xml>
複製代碼
技術交流
My Blog
blog.guijianpan.comphp