php微信接收消息

微信接受文本消息

微信官方文档

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
vaild();
function (){
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$echostr = $_GET["echostr"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature && echostr){
echo $echostr;
}else{
return false;
}
}
responseMsg();
function responseMsg(){
$info = file_get_contents("php://input");
$postObj = simplexml_load_string($info);
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$time = time();
$msgType = $postObj->MsgType;
$content = '你好啊';
$template = '<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>';
$response = sprintf($template,$toUser,$fromUser,$time,$msgType,$content);
file_put_contents('./response.html', $response);
echo $response;
}

注:关于使用$_GLOBALS['HTTP_RAW_POST_DATA']取不到值得问题,需要在php.ini,中另行配置,这种用法在新版本中已经说明以后不会再用,用file_get_contents("php://input")来获得