前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >20行代码搞定发送短信测试

20行代码搞定发送短信测试

原创
作者头像
成蝶短信
发布2025-01-09 13:35:01
发布2025-01-09 13:35:01
860
举报

在制作产品推广的时候,由于我们是产品部门没有专门的技术,用短信做推广,为了货比三家,在腾讯千帆云市场找了一家短信服务商,这家服务商很贴心,给我们小白直接贴20行以内代码,直接用shell脚本就能搞定测试,下面和大家分享一下牛逼的代码。

++++++++++++++++++++++++++JAVA+++++++++++++++++++++

// 发送短信

String sendUrl = "https://api.cxdx.cn/api/send/sms";

Map<String, String> map = new HashMap();

String timestamp = String.valueOf(System.currentTimeMillis());

String signature = SecretUtil.MD5(account + timestamp + apiKey);

map.put("account", "N57***"); // API账号

map.put("phones", "137******"); // 手机号

map.put("signName", "签名"); // 签名

map.put("templateCode", "SPT_411651***"); // 模版编号

map.put("templateParam", "{\"name\":\"张三\",\"code\":\"123456\"}"); // 模版变量

map.put("timestamp", timestamp); //时间戳

map.put("signature", signature); //MD5加密串

//发送请求

JSONObject js = (JSONObject) JSONObject.toJSON(map);

String body = cn.hutool.http.HttpUtil.post(sendUrl, js.toString(), 5000);

System.out.println(body);

++++++++++++++++++++++++++PHP+++++++++++++++++++++

const API_SEND_URL ='https://api.cxdx.cn/api/send/sms';

const API_ACCOUNT = 'N57***';

const API_KEY = '7383CC25C***';

public function sendSMS(){

$timestamp = time();

$postArr = [

'account' => self::API_ACCOUNT,

'phones' => '137******',//手机号码

'signName' => '签名',//签名

'templateCode' => 'SPT_411651***',//模板编号

'templateParam' => '{"name":"张三","code":"123456"}',//模板变量

'timestamp' => $timestamp,

'signature' => md5(self::API_ACCOUNT . $timestamp . self::API_KEY)

];

return $this->curlPost(self::API_SEND_URL, $postArr);

}

++++++++++++++++++++++++++Python+++++++++++++++++++++

def send_sms():

md5String = hashlib.md5((str(account + timestamp + apiKey)).encode(encoding='utf-8')).hexdigest()

params = {

'account': 'N57', //API账号

'phones' : '137******', //手机号码

'signName': '签名', //签名

'templateCode': 'SPT_411651***', //模板编号

'templateParam' : '{"name":"张三","code":"123456"}', //模板变量

'timestamp' : '1362662154211', //当前毫秒时间戳

'signature' : md5String //标准MD5加密

}

params=json.dumps(params)

headers = {"Content-type": "application/json"}

conn = httplib.HTTPConnection(host, port=port, timeout=30)

conn.request("POST", sms_send_uri, params, headers)

response = conn.getresponse()

response_str = response.read()

conn.close()

return response_str

++++++++++++++++++++++++++Go+++++++++++++++++++++

params := make(map[string]interface{})

params["account"] = "N57***" //API账号

params["phones"] = "137******" //手机号码

params["signName"] = "签名" //签名

params["templateCode"] = "SPT_411651***" //模板编号

params["templateParam"] = "{\"name\":\"张三\",\"code\":\"123456\"}" //模板变量

params["timestamp"] = "1362662154211" //当前毫秒时间戳

data := []byte(account + timestamp + apiKey)

md5New := md5.New()

md5New.Write(data)

md5String := hex.EncodeToString(md5New.Sum(nil)) // hex转字符串

params["signature"] = md5String

bytesData, err := json.Marshal(params)

reader := bytes.NewReader(bytesData)

url := "https://api.cxdx.cn/api/send/sms" //短信发送URL

request, err := http.NewRequest("POST", url, reader)

request.Header.Set("Content-Type", "application/json;charset=UTF-8")

client := http.Client{}

resp, err := client.Do(request)

respBytes, err := ioutil.ReadAll(resp.Body)

str := (*string)(unsafe.Pointer(&respBytes))

fmt.Println(*str)

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档