本文原创自news.mkq.online
本文地址:news.wpj.online
版权声明:本文为原创文章,版权牛站www.niuzhan.com所有
本文实例为大家分享了php微信开发之关键词回复的具体代码,供大家参考,具体内容如下
view sourceprint?
01
valid();
10
$wechatObj->responseMsg();
11
class wechatCallbackapiTest
12
{
13
public function valid()
14
{
15
$echoStr = $_GET["echostr"];
16
17
//valid signature , option
18
if($this->checkSignature()){
19
echo $echoStr;
20
exit;
21
}
22
}
23
24
public function responseMsg()
25
{
26
//get post data, May be due to the different environments
27
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
28
29
//extract post data
30
if (!empty($postStr)){
31
32
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
33
$fromUsername = $postObj->FromUserName;
34
$toUsername = $postObj->ToUserName;
35
$keyword = trim($postObj->Content);
36
$time = time();
37
$textTpl = "%s0";
38
if(!empty( $keyword ))
39
{
40
$msgType = "text";
41
if($keyword=="1"){
42
$contentStr = "qiphon";}
43
if($keyword=="2"){
44
$contentStr = "最新资讯:《环境违法行为举报奖励办法》";}
45
if($keyword=="3"){
46
$contentStr = "31离婚协议\n32租房合同\n33交通事故协议书";}
47
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
48
echo $resultStr;
49
}else{
50
echo "Input something...";
51
}
52
53
}else {
54
echo "";
55
exit;
56
}
57
}
58
59
private function checkSignature()
60
{
61
$signature = $_GET["signature"];
62
$timestamp = $_GET["timestamp"];
63
$nonce = $_GET["nonce"];
64
65
$token = TOKEN;
66
$tmpArr = array($token, $timestamp, $nonce);
67
sort($tmpArr);
68
$tmpStr = implode( $tmpArr );
69
$tmpStr = sha1( $tmpStr );
70
71
if( $tmpStr == $signature ){
72
return true;
73
}else{
74
return false;
75
}
76
}
77
}
78
79
?>
以上就是本文的全部内容,希望对大家的学习有所帮助。