forked from kaixin1995/InformationPush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
work.php
88 lines (78 loc) · 2.46 KB
/
work.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
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
<?php
date_default_timezone_set('PRC');
//如果不存在文本就禁止提交
if(!isset($_REQUEST['msg']))
{
exit;
}
//获取发送数据数组
function getDataArray($MsgArray)
{
$data = array(
//要发送给的用户,@all为全部
"touser" => "@all",
"toparty" => "@all",
"totag" => "@all",
"msgtype" => "textcard",
//改成自己的应用id
"agentid" => $MsgArray["agentid"],
'textcard' => array(
'title' => $MsgArray["title"],
//提示的时间和内容
'description' => '<div class="gray">'.$MsgArray["time"].'</div> <div class="normal">'.$MsgArray["msg"].'</div>',
//点击模板打开的链接
'url' => $MsgArray["url"],
"btntxt"=>"详情"
)
);
return $data;
}
//curl请求函数,微信都是通过该函数请求
function https_request($url, $data = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
/**
* 开始推送
*/
//替换你的ACCESS_TOKEN
$ACCESS_TOKEN = json_decode(https_request("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=企业ID&corpsecret=应用的Secret"),true)["access_token"];
//模板消息请求URL
$url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=".$ACCESS_TOKEN;
$MsgArray=array();
//推送的应用id
$MsgArray["agentid"]="";
//标题是可选值
if(!isset($_REQUEST['title'])){
$MsgArray["title"]="新提醒";
}
else{
$MsgArray["title"]=$_REQUEST['title'];
}
//推送的文本内容
$MsgArray["msg"]=$_REQUEST['msg'];
//推送时间
$MsgArray["time"]=date('Y-m-d h:i:s',time());
$MsgArray["url"]="http://script.haokaikai.cn/Remind/msg.php?title=".$MsgArray["title"]."&time=".$MsgArray["time"]."&msg=".$MsgArray["msg"];
//转化成json数组让微信可以接收
$json_data = json_encode(getDataArray($MsgArray));
//echo $json_data;exit;
$res = https_request($url, urldecode($json_data));//请求开始
$res = json_decode($res, true);
if ($res['errcode'] == 0 && $res['errcode'] == "ok") {
echo "发送成功!<br/>";
}
else{
echo "发送失败<br/>";
}