短信營銷接口文檔 點(diǎn)擊下載
短信營銷接口接入指南 點(diǎn)擊訪問
//golang版本Demo
//實(shí)現(xiàn)任務(wù)提交以及獲取余額
package main
import (
"bytes"
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"sort"
"strings"
"time"
)
//批量發(fā)送
func batchSend() {
type sendData struct {
APIID string `json:"api_id"`
Signature string `json:"signature"`
Timestamp int `json:"timestamp"`
RequestID string `json:"request_id"`
ProductID int `json:"product_id"` //產(chǎn)品ID
Phone []string `json:"phone"` //手機(jī)號
SignName string `json:"sign_name"` //短信簽名
Content string `json:"content"` //短信內(nèi)容
TemplateID int `json:"template_id"` //模板ID
TemplateVar map[string]string `json:"template_var"` //模板變量
SendTime string `json:"send_time"` //定時時間
}
data := sendData{}
data.APIID = "xxxxxx"
data.RequestID = randomString(20, "Aa0")
data.Timestamp = int(time.Now().Unix())
data.Phone = []string{"18801000000", "13101011111"}
data.ProductID = 1013
data.Content = "這是短信內(nèi)容"
data.SignName = "這是簽名"
signData := make(map[string]string)
signData["api_id"] = data.APIID
signData["timestamp"] = fmt.Sprintf("%d", data.Timestamp)
signData["request_id"] = data.RequestID
data.Signature = sign(signData, "xxxxxx")
url := "https://api.ihuyi.com/sms-yx/v1/batchSend"
client := &http.Client{
Timeout: time.Second * 10,
}
sendRawData, _ := json.Marshal(data)
request, err := http.NewRequest("POST", url, strings.NewReader(string(sendRawData)))
if err != nil {
fmt.Print(err)
return
}
request.Header.Set("Content-Type", "application/json")
response, err := client.Do(request)
if err != nil {
fmt.Print(err)
return
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Print(err)
return
}
getData := string(body)
fmt.Println(getData)
}
//獲取余額
func balance() {
type sendData struct {
APIID string `json:"api_id"`
Signature string `json:"signature"`
Timestamp int `json:"timestamp"`
RequestID string `json:"request_id"`
}
data := sendData{}
data.APIID = "xxxxxxx"
data.RequestID = randomString(20, "Aa0")
data.Timestamp = int(time.Now().Unix())
signData := make(map[string]string)
signData["api_id"] = data.APIID
signData["timestamp"] = fmt.Sprintf("%d", data.Timestamp)
signData["request_id"] = data.RequestID
data.Signature = sign(signData, "xxxxxxx")
url := "https://api.ihuyi.com/sms-yx/v1/balance"
client := &http.Client{
Timeout: time.Second * 10,
}
sendRawData, _ := json.Marshal(data)
request, err := http.NewRequest("POST", url, strings.NewReader(string(sendRawData)))
if err != nil {
fmt.Print(err)
return
}
request.Header.Set("Content-Type", "application/json")
response, err := client.Do(request)
if err != nil {
fmt.Print(err)
return
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Print(err)
return
}
getData := string(body)
fmt.Println(getData)
}
//getMd5String 返回md5加密字符串
func getMd5String(s string) string {
h := md5.New()
h.Write([]byte(s))
return hex.EncodeToString(h.Sum(nil))
}
//獲取簽名
func sign(data map[string]string, appKEY string) string {
var keys []string
data["api_key"] = appKEY
for k := range data {
keys = append(keys, k)
}
sort.Strings(keys)
var hash []string
for _, v := range keys {
hash = append(hash, fmt.Sprintf("%s=%s", v, data[v]))
}
return getMd5String(strings.Join(hash, "&"))
}
//RandomString 隨機(jī)生成字符串
//RandomString(8, "A")
//RandomString(8, "a0")
//RandomString(20, "Aa0")
func randomString(randLength int, randType string) (result string) {
var num = "0123456789"
var lower = "abcdefghijklmnopqrstuvwxyz"
var upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
b := bytes.Buffer{}
if strings.Contains(randType, "0") {
b.WriteString(num)
}
if strings.Contains(randType, "a") {
b.WriteString(lower)
}
if strings.Contains(randType, "A") {
b.WriteString(upper)
}
var str = b.String()
var strLen = len(str)
if strLen == 0 {
result = ""
return
}
rand.Seed(time.Now().UnixNano())
b = bytes.Buffer{}
for i := 0; i < randLength; i++ {
b.WriteByte(str[rand.Intn(strLen)])
}
result = b.String()
return
}
func main() {
batchSend()
balance()
}
驗(yàn)證碼已發(fā)送到您的手機(jī),請查收!
輸入驗(yàn)證碼后,點(diǎn)擊“開通體驗(yàn)賬戶”按鈕可立即開通體驗(yàn)賬戶。