互亿无线 · 文档中心

短信营销 - 代码示例


GO对接短信营销接口DEMO示例
本文为您提供了GO语言版本的短信营销接口对接DEMO示例

短信营销接口文档 点击下载

短信营销接口接入指南 点击访问

//golang版本Demo
//实现任务提交以及获取余额
package main

import (
	"bytes"
	"crypto/md5"
	"encoding/hex"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"math/rand"
	"net/http"
	"sort"
	"strings"
	"time"
)

//批量发送
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"`   //产品ID
		Phone       []string          `json:"phone"`        //手机号
		SignName    string            `json:"sign_name"`    //短信签名
		Content     string            `json:"content"`      //短信内容
		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 = "这是短信内容"
	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 随机生成字符串
//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()
}

现在注册,即享新用户礼包!

在线咨询
电话咨询

服务热线:

4008 808 898

服务热线(工作时间):

4008 808 898

业务咨询(非工作时间):

售后咨询(非工作时间):

验证码已发送到您的手机,请查收!

输入验证码后,点击“开通体验账户”按钮可立即开通体验账户。

收不到短信验证码?
互亿无线不提供接码服务,接码用户请勿点击开通按钮。
×