移动短信发送的php和go实现demo

说明

对接移动短信发送的sdk,官方是java版本的。
现在用自己熟悉的php和新学的go来实现一遍,看看go的实现有和不同

官方java版本的demo

demoforjava

php实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public function sendsms(){

$url = 'http://112.35.1.155:1992/sms/norsubmit';
$client = new HttpClient($url);

$ecName = "管理中心";
$apId = "xxx";
$secretKey = "L00001";
$mobiles = "136****00";
$content = "移动改变生活";
$sign = "xcxxcxcxc";
$addSerial = "";
$mac_origin = $ecName.$apId.$secretKey.$mobiles.$content.$sign.$addSerial;

$mac = strtolower(md5($mac_origin));

$object = new \stdClass();
$object->ecName = $ecName;
$object->apId = $apId;
$object->secretKey = $secretKey;
$object->mobiles = $mobiles;
$object->content = $content;
$object->sign = $sign;
$object->addSerial = $addSerial;
$object->mac = $mac;

$json = json_encode($object,JSON_FORCE_OBJECT);

$message = base64_encode($json);

$client->setContentType("application/json; charset=UTF-8");
$response = $client->post($message);

var_dump($response);
}

go的实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main

import (
"fmt"
"strings"
"net/http"
"io/ioutil"
"crypto/md5"
"encoding/hex"
"encoding/json"
"encoding/base64"
// "reflect"
)

type Object struct{
EcName string `json:"ecName"`
ApId string `json:"apId"`
SecretKey string `json:"secretKey"`
Mobiles string `json:"mobiles"`
Content string `json:"content"`
Sign string `json:"sign"`
AddSerial string `json:"addserial"`
Mac string `json:"mac"`
}

func mkmd5(s string) string {
md5Ctx := md5.New()
md5Ctx.Write([]byte(s))
cipherStr := md5Ctx.Sum(nil)
return hex.EncodeToString(cipherStr)
}

func main() {
client := &http.Client{}
posturl := "http://112.35.1.155:1992/sms/norsubmit"

ecName := "管理中心";
apId := "xxx";
secretKey := "L00001";
mobiles := "136****00";
content := "移动改变生活";
sign := "xcxxcxcxc";
addSerial := "";
mac_origin := ecName + apId + secretKey + mobiles + content + sign + addSerial;

mac := mkmd5(mac_origin)

object := Object{
EcName: ecName,
ApId:apId,
SecretKey:secretKey,
Mobiles:mobiles,
Content:content,
Sign:sign,
AddSerial:addSerial,
Mac:mac,
}

sdata, err := json.Marshal(object)
fmt.Println(sdata)
// fmt.Println("sdata 的数据类型是:",reflect.TypeOf(sdata))

if err != nil {
fmt.Println("json.marshal failed, err:", err)
return
}

// json_string := string(sdata)
// fmt.Println(json_string)

message := base64.StdEncoding.EncodeToString(sdata)

req, err := http.NewRequest("POST", posturl, strings.NewReader(message))
if err != nil {
// handle error
}

req.Header.Set("Content-Type", "application/json")

resp, err := client.Do(req)
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
// handle error
}
fmt.Println(string(body))
}

主要的不同

  • md5的方法需要自己去封装下使用
  • json.Marshal返回的是 []uint8,需要自己再转格式
  • go语音在使用类似php中的函数时,必须自己清楚需要用到哪些包

存在的坑

  • golang中struct变量JSON转码变量名必须大写

参考

Golang计算MD5

golang中struct变量JSON转码变量名必须大写

golang实现base64编解码