-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwx_sample.php
237 lines (203 loc) · 6.06 KB
/
wx_sample.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
/**
* 微信公众平台接口
*/
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->weixin_run(); //执行接收器方法
class wechatCallbackapiTest
{
private $fromUsername;
private $toUsername;
private $times;
private $keyword;
public function getdatearray(){
$keyword = trim($this->keyword);
$FromUserName = $this->fromUsername;
$ToUserName = $this->toUsername;
if(!empty( $keyword ))
{
require_once "./getdatedemo.php";
$test = new getdatedemo();
$contentStr = $test->newsinfo($keyword);
if(!$contentStr)
{
if(!$contentStr)
{
$contentStr = $test->keysinfo($keyword);
if(!$contentStr)
{
$contentStr = $test->validnum($keyword);
if(!$contentstr)
{
echo "";
exit;
}
}
}
}
return $contentStr;
}
else
{
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
$msgType = "text";
require_once( dirname(__FILE__).'/weixindemoAdmin2013/topic/config/config.inc.php');
$conn=mysql_connect($config['dbhost'],$config['dbuser'],$config['dbpass']);
$flag=mysql_select_db($config['dbname'],$conn);
mysql_query("set names utf8");
$sql = mysql_query("SELECT * FROM `tb_reply_news` arc where arc.id=1");
$nofind = mysql_fetch_array($sql);
$title=$nofind['order'];
$title = str_replace(" ","",$title);
$title = str_replace("<p>","",$title);
$title = str_replace("</p>","",$title);
$title = preg_replace('/^\s+\r?\n/m',"\n", $title);
//$title = htmlspecialchars_decode($title);
$contentStr = $title;
$resultStr = sprintf($textTpl, $FromUserName, $ToUserName, $time, $msgType, $contentStr);
echo $resultStr;
exit;
}
}
public function weixin_run(){
$this->responseMsg();
$arr=$this->getdatearray();
if(! is_array($arr))
{
$rarr[]=$arr;
$this->fun_xml("text",$rarr,array(0));
}
elseif(is_array($arr) && $arr['type']=="news")
{
$this->fun_xml("news",$arr,array($arr['numberlist'],0));
}
else
{
$contentStr = array("");//查找不到内容返回空
$this->fun_xml("text",$contentStr,array(0));
}
}
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$this->fromUsername = $postObj->FromUserName;
$this->toUsername = $postObj->ToUserName;
$this->keyword = trim($postObj->Content);
$this->times = time();
}else {
echo "";
exit;
}
}
//微信封装类,
//type: text 文本类型, news 图文类型
//text,array(内容),array(ID)
//news,array(array(标题,介绍,图片,超链接),...小于10条),array(条数,ID)
private function fun_xml($type,$value_arr,$o_arr=array(0)){
//=================xml header============
$con="<xml>
<ToUserName><![CDATA[{$this->fromUsername}]]></ToUserName>
<FromUserName><![CDATA[{$this->toUsername}]]></FromUserName>
<CreateTime>{$this->times}</CreateTime>
<MsgType><![CDATA[{$type}]]></MsgType>";
//=================type content============
switch($type){
case "text" :
$con.="<Content><![CDATA[{$value_arr[0]}]]></Content>
<FuncFlag>{$o_arr}</FuncFlag>";
break;
case "news" :
$con.="<ArticleCount>{$o_arr[0]}</ArticleCount>
<Articles>";
foreach($value_arr as $id=>$v){
if($id>=$o_arr[0]) break; else null; //判断数组数不超过设置数
$con.="<item>
<Title><![CDATA[{$v[title]}]]></Title>
<Description><![CDATA[{$v[description]}]]></Description>
<PicUrl><![CDATA[{$v[picurl]}]]></PicUrl>
<Url><![CDATA[{$v[url]}]]></Url>
</item>";
}
$con.="</Articles>
<FuncFlag>{$o_arr[1]}</FuncFlag>";
break;
} //end switch
//=================end return============
echo $con."</xml>";
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
define("ACCESS_TOKEN", 'HVFP4ZtEHAf6mm3NeJMoaq6GM2-dArGlP3UwymcbJBlvVZUVJ1lcp6aDmeOHcCApaoGKF_DaxrYpG9wkeUOaoXm7-IiqpfvJ1KSLyLTiWPA');
function createMenu($data){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".ACCESS_TOKEN);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);
if (curl_errno($ch)) {
return curl_error($ch);
}
curl_close($ch);
return $tmpInfo;
}
$data = ' {
"button":[
{
"type":"view",
"name":"文章目录",
"url":"http://mp.weixin.qq.com/s?__biz=MjM5NzA1MTcyMA==&mid=100123036&idx=1&sn=5cce1f220cfb9bd1abd494b858263b56#rd"
},
{
"type":"view",
"name":"公号推荐",
"url":"http://mp.weixin.qq.com/s?__biz=MjM5NzA1MTcyMA==&mid=207634230&idx=1&sn=2b001b99848fbb3d5fe5963ab5d4ab46#rd"
},
{
"type":"view",
"name":"联系我们",
"url":"http://mp.weixin.qq.com/s?__biz=MjM5NzA1MTcyMA==&mid=200153165&idx=1&sn=d2c9e846bec0e87180f7dae1b892780f#rd"
}
]
}';
echo createMenu($data);//创建菜单
?>