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
|
<?php
/*
http://fanyi.youdao.com/openapi.do?keyfrom=weifan&key=791593512&type=data&doctype=<doctype>&version=1.1&q=要翻译的文本
方倍工作室 http://www.cnblogs.com/txw1958/
CopyRight 2013 www.doucube.com All Rights Reserved
*/
traceHttp();
define(
"TOKEN"
,
"weixin"
);
$wechatObj
=
new
wechatCallbackapiTest();
if
(isset(
$_GET
[
'echostr'
])) {
$wechatObj
->valid();
}
else
{
$wechatObj
->responseMsg();
}
class
wechatCallbackapiTest
{
public
function
valid()
{
$echoStr
=
$_GET
[
"echostr"
];
if
(
$this
->checkSignature()){
echo
$echoStr
;
exit
;
}
}
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;
}
}
public
function
responseMsg()
{
$postStr
=
$GLOBALS
[
"HTTP_RAW_POST_DATA"
];
//simplexml_load_string() 函数把 XML 字符串载入对象中。
if
(!
empty
(
$postStr
)){
$postObj
= simplexml_load_string(
$postStr
,
'SimpleXMLElement'
, LIBXML_NOCDATA);
$fromUsername
=
$postObj
->FromUserName;
$toUsername
=
$postObj
->ToUserName;
$keyword
= trim(
$postObj
->Content);
$time
= time();
$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>";
//sprintf() 函数把格式化的字符串写入一个变量中
//strstr() 函数搜索一个字符串在另一个字符串中的第一次出现
if
(
$keyword
==
"1"
)
{
$msgType
=
"text"
;
$contentStr
=
"哈哈"
;
$resultStr
= sprintf(
$textTpl
,
$fromUsername
,
$toUsername
,
$time
,
$msgType
,
$contentStr
);
echo
$resultStr
;
}
elseif
(
$keyword
==
"A"
)
{
$msgType
=
"text"
;
$contentStr
=
"OK"
;
$resultStr
= sprintf(
$textTpl
,
$fromUsername
,
$toUsername
,
$time
,
$msgType
,
$contentStr
);
echo
$resultStr
;
}
elseif
(
$keyword
==
"B"
)
{
$msgType
=
"text"
;
$contentStr
=
"good"
;
$resultStr
= sprintf(
$textTpl
,
$fromUsername
,
$toUsername
,
$time
,
$msgType
,
$contentStr
);
echo
$resultStr
;
}
else
if
(
$keyword
==
"fff"
){
$msgType
=
"text"
;
$contentStr
=
"asasasas!!<font color = 'red'>"
.
$keyword
.
"</font>zzzz"
;
$resultStr
= sprintf(
$textTpl
,
$fromUsername
,
$toUsername
,
$time
,
$msgType
,
$contentStr
);
echo
$resultStr
;
exit
();
}
}
else
{
$msgType
=
"text"
;
$contentStr
=
$keyword
.
"zzzz"
;
$resultStr
= sprintf(
$textTpl
,
$fromUsername
,
$toUsername
,
$time
,
$msgType
,
$contentStr
);
echo
$resultStr
;
exit
;
}
}
}
function
traceHttp()
{
//“REMOTE_ADDR”:正在浏览当前页面用户的 IP 地址。
logger(
"\n\nREMOTE_ADDR:"
.
$_SERVER
[
"REMOTE_ADDR"
].(
strstr
(
$_SERVER
[
"REMOTE_ADDR"
],
'101.226'
)?
" FROM WeiXin"
:
"Unknown IP"
));
logger(
"QUERY_STRING:"
.
$_SERVER
[
"QUERY_STRING"
]);
}
function
logger(
$log_content
)
{
if
(isset(
$_SERVER
[
'HTTP_APPNAME'
])){
//SAE
sae_set_display_errors(false);
//
sae_debug(
$log_content
);
//sae_debug是保存到日志里面, 不会保存到storage里面.
sae_set_display_errors(true);
}
else
{
//LOCAL
$max_size
= 500000;
$log_filename
=
"weixinlog.xml"
;
if
(
file_exists
(
$log_filename
)
and
(
abs
(
filesize
(
$log_filename
)) >
$max_size
)){unlink(
$log_filename
);}
file_put_contents
(
$log_filename
,
date
(
'Y-m-d H:i:s'
).
$log_content
.
"\r\n"
, FILE_APPEND);
}
}
?>
|
本文转自噼里啪啦啦 51CTO博客,原文链接:http://blog.51cto.com/pilipala/1661398
,如需转载请自行联系原作者