1. 看你们的项目选择什么平台,不同平台调用Api的方法不同。
2. 首先登录互亿无线平台https://www.ihuyi.com/duanxin.html
如果你只是练习购买学生专享套餐就可以,如果是企业项目,根据需求购买短信条数
【我当时没搞明白,联系了互亿的后台,感谢平台送了我10条】
3.将代码拉入自己的项目中
// 隐私内容我用******代替
//接口类型:互亿无线触发短信接口,支持发送验证码短信、订单通知短信等。
// 账户注册:请通过该地址开通账户http://sms.ihuyi.com/register.html
// 注意事项:
//(1)调试期间,请用默认的模板进行测试,默认模板详见接口文档;
//(2)请使用APIID(查看APIID请登录用户中心->验证码短信->产品总览->APIID)及 APIkey来调用接口;
//(3)该代码仅供接入互亿无线短信接口参考使用,客户可根据实际需要自行编写;
// https://www.ihuyi.com/demo/sms/go.html
package common
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
type Sms struct {}
func GetMd5String(s string) string {
h := md5.New()
h.Write([]byte(s))
return hex.EncodeToString(h.Sum(nil))
}
func SmsResult() (string,error) {
v := url.Values{}
_now := strconv.FormatInt(time.Now().Unix(), 10)
//fmt.Printf(_now)
_account := "C9******2" //用户名是登录用户中心->验证码短信->产品总览->APIID
_password := "2f78******5ec43a5e" //查看密码请登录用户中心->验证码短信->产品总览->APIKEY
_mobile := "157******8" // 手机号
_content := "您的验证码是:9968。请不要把验证码泄露给其他人。"
v.Set("account", _account)
v.Set("password", GetMd5String(_account+_password+_mobile+_content+_now))
v.Set("mobile", _mobile)
v.Set("content", _content)
v.Set("time", _now)
//body := ioutil.NopCloser(strings.NewReader(v.Encode())) //把form数据编下码
body := strings.NewReader(v.Encode()) //把form数据编下码
client := &http.Client{}
req, _ := http.NewRequest("POST", "http://106.ihuyi.com/webservice/sms.php?method=Submit&format=json", body) // 互亿平台的Api
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
resp, err := client.Do(req) //发送
defer resp.Body.Close() //一定要关闭resp.Body
data, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(data), err)
return string(data),err
}
Controller调用
//发送短信
func (c *Register) SendSms(r *ghttp.Request) {
result, err := SmsResult()
if err != nil {
return
}
response.SusJson(true, r, "成功", result)
}
接口访问
查看自己账号上还有几条可发短信