-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsendmsg.php
More file actions
149 lines (135 loc) · 4.47 KB
/
sendmsg.php
File metadata and controls
149 lines (135 loc) · 4.47 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
include_once "YdApi.php";
include_once "Const.php";
$ydapi = new YdApi(AESKEY, APPID, BUIN);
/*
获取token
*/
$result = $ydapi->EncryptMsg(time());
list($errcode, $encrypt) = $ydapi->EncryptMsg($msg);
if ($errcode === 0) {
list($errcode, $rsp) = $ydapi->GetToken(API_GET_TOKEN, $encrypt);
if ($errcode === 0) {
echo "获取 token: $rsp 成功<br>";
} else {
echo "获取 token 失败 errcode : $errcode , errmsg : $rsp <br/>";
exit();
}
}
$mediaId = '';
/*
上传文件
*/
$fname = 'debug.log';
if (!file_exists($fname)) {
echo "文件 $fname 不存在";
exit();
}
$file = file_get_contents($fpath);
$msg = json_encode(['type' => 'file', 'name' => $fname, 'buin'=> BUIN, 'appId'=> APPID]);
list($errcode1, $encrypt_msg) = $ydapi->EncryptMsg($msg);
list($errcode2, $encrypt_file) = $ydapi->EncryptMsg($file);
if ($errcode1 === 0 && $errcode2 === 0) {
list($errcode1, $mId) = $ydapi->UploadFile(API_UPLOAD_FILE, $encrypt_msg, $encrypt_file, $fname);
if ($errcode1 === 0) {
echo "上传文件成功 fileId : $mId <br/>";
$mediaId = $mId;
} else {
echo "上传文件失败 errcode : $errcode1 <br/>";
exit();
}
} else {
echo "加密失败";
exit();
}
/*
上传图片
*/
$fname = 'sample.png';
if (!file_exists($fname)) {
echo "文件 $fname 不存在";
exit();
}
$file = file_get_contents($fname);
$msg = json_encode(['type' => 'image', 'name' => $fname, 'buin'=> BUIN, 'appId'=> APPID]);
list($errcode1, $encrypt_msg) = $ydapi->EncryptMsg($msg);
list($errcode2, $encrypt_file) = $ydapi->EncryptMsg($file);
if ($errcode1 === 0 && $errcode2 === 0) {
list($errcode1, $mId) = $ydapi->UploadFile(API_UPLOAD_FILE, $encrypt_msg, $encrypt_file, $fname);
if ($errcode1 === 0) {
echo "上传图片成功 mediaId : $mId <br/>";
$mediaId = $mId;
} else {
echo "上传图片失败 errcode : $errcode1 <br/>";
exit();
}
} else {
echo "加密失败";
exit();
}
/*
发送文字信息
*/
$msg = '{"toUser": "dico.zhang","msgType":"text","text":{"content": "it"}}';
$rsp = $ydapi->Send(API_SEND_MSG, $msg);
if ($rsp["errcode"] === 0) {
echo "发送文字信息成功 ". $rsp["decrypt"]["id"] ."<br/>";
} else {
echo "发送文字信息失败 errcode: ". $rsp["errcode"] .", errmsg: ". $rsp["errmsg"] ."<br/>";
exit();
}
/*
发送图片信息
*/
$msg = '{"toUser": "dico.zhang","msgType":"image","image":{"media_id": "'.$mediaId.'"}}';
$rsp = $ydapi->Send(API_SEND_MSG, $msg);
if ($rsp["errcode"] === 0) {
echo '发送图片信息成功<br/>';
} else {
echo "发送图片信息失败 errcode: $errcode, errmsg: $rsp<br/>";
exit();
}
/*
发送文件信息
*/
$msg = '{"toUser": "dico.zhang","msgType":"file","file":{"media_id": "'.$mediaId.'"}}';
$rsp = $ydapi->Send(API_SEND_MSG, $msg);
if ($rsp["errcode"] === 0) {
echo '发送文件信息成功<br/>';
} else {
echo "发送文件信息失败 errcode: $errcode, errmsg: $rsp<br/>";
exit();
}
/*
发送图文信息
*/
$msg = '{"toUser": "dico.zhang","msgType":"mpnews","mpnews":[{"title": "这里是标题","media_id": "'.$mediaId.'","digest": "这里是简介","content": "这里是内容","showFront": 1,"url": "http://www.baidu.com"}]}';
$rsp = $ydapi->Send(API_SEND_MSG, $msg);
if ($rsp["errcode"] === 0) {
echo '发送图文信息成功<br/>';
} else {
echo "发送图文信息失败 errcode: $errcode, errmsg: $rsp<br/>";
exit();
}
/*
发送外链信息
*/
$msg = '{"toUser": "dico.zhang","msgType":"exlink","exlink":[{"title": "这里是标题","media_id": "'.$mediaId.'","digest": "这里是简介","url": "http://www.baidu.com"}]}';
$rsp = $ydapi->Send(API_SEND_MSG, $msg);
if ($rsp["errcode"] === 0) {
echo '发送图文信息成功<br/>';
} else {
echo "发送图文信息失败 errcode: $errcode, errmsg: $rsp<br/>";
exit();
}
/*
发送应用弹窗
*/
$msg = '{"toUser": "dico.zhang","toDept": "","popWindow":{"url": "http://youdu.im","tip": "","title": "应用弹窗","width": 400,"height": 400,"duration": -1,"position": 3,"notice_id": "{202D2081-5ADA-4FE5-81B8-53D88E1FD016}","pop_mode": 2}}';
$rsp = $ydapi->Send(API_SEND_MSG, $msg);
if ($rsp["errcode"] === 0) {
echo '发送应用弹窗成功<br/>';
} else {
echo "发送应用弹窗失败 errcode: $errcode, errmsg: $rsp<br/>";
exit();
}