-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwxinterface.php
131 lines (109 loc) · 3.37 KB
/
wxinterface.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
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
<?php
require_once "dbconn.php";
function getUrl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22");
curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); //加入gzip解析
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
function postUrl($url, $postData)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$response = curl_exec($ch);
if (curl_errno($ch)) {
print curl_error($ch);
}
curl_close($ch);
return $response;
}
function getAccessToken()
{
$token = getTokenFromDb();
if ($token != "0") {
return $token;
}
$tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . WX_APPID . "&secret=" . WX_APPSECRET;
$content = getUrl($tokenUrl);
$content = json_decode($content, true);
$newtoken = $content["access_token"];
saveTokenToDb($newtoken);
return $newtoken;
}
function getUserId($code)
{
//$token = getAccessToken();
$useridurl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . WX_APPID .
"&secret=" . WX_APPSECRET .
"&code=" . $code .
"&grant_type=authorization_code";
$content = getUrl($useridurl);
$result = json_decode($content, true);
return $result;
}
function getUserInfoFromWx($userAccesstoken, $openId)
{
$userInfoUrl = "https://api.weixin.qq.com/sns/userinfo" .
"?access_token=" . $userAccesstoken .
"&openid=" . $openId .
"&lang=zh_CN";
$result = getUrl($userInfoUrl);
return json_decode($result, true);
}
function sendMsg($userid, $msg)
{
{
/**
* $token = getAccessToken();
*
* $url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" . $token;
*
* //echo "url=" . $url . "<br />";
* $msg = $msg . "\n<a href=\"" . DOMAIN . "/datiaffirm.php\">继续答题</a>";
* //"\n<a href=\"" . DOMAIN . "/anslist.php\">查看排行榜</a>";
*
* $arr = array(
* 'touser' => $userid,
* 'msgtype' => 'text',
* 'agentid' => '1000004',
* 'text' => array(
* 'content' => $msg
* ),
* 'safe' => '0'
* );
*
* $arr = json_encode($arr);
* postUrl($url, $arr);
*/
}
}
function getJsAPITicket()
{
$ticket = getJsAPITicketFromDb();
if ($ticket == "0") {
//已过期重新获取
$token = getAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" . $token . "&type=jsapi";
$result = getUrl($url);
$result = json_decode($result, true);
$ticketnew = $result['ticket'];
if (!isset($ticketnew)) {
die("获取jsticket失败");
}
$ticket = $ticketnew;
saveJsAPITicketToDb($ticket);
}
return $ticket;
}