微信公众平台实现接口验证和关注后回复+智能被动回复。

解决了php不能回复用户消息的问题。

 

$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];注释掉

$postStr = file_get_contents("php://input");//因为很多都设置了register_globals禁止,不能用$GLOBALS["HTTP_RAW_POST_DATA"];

 

完整代码如下:

 

<?php
/**
* wechat php test
*/

//define your token
define("TOKEN", "hellowangliqiang");
$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();//接口验证
$wechatObj->responseMsg();//调用回复消息方法
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];

//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}

public function responseMsg()
{
// $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
$postStr = file_get_contents("php://input");

if (!empty($postStr)){
/* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
the best way is to check the validity of xml by yourself */
libxml_disable_entity_loader(true);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$msgType = $postObj->MsgType;//消息类型
$event = $postObj->Event;//时间类型,subscribe(订阅)、unsubscribe(取消订阅)
$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>";

switch($msgType){
case "event":
if($event=="subscribe"){
$contentStr = "Hi,欢迎关注哦,这是一个代码测试账号!"."\n"."回复数字1"."\n"."回复数字2";
}
break;
case "text":
switch($keyword){
case "1":
$contentStr = "数字1回复的内容";
break;
case "2":
$contentStr = "数字2回复的内容";
break;
default:
$contentStr = "对不起,你的内容我会稍后回复";
}
break;
}
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else {
echo "";
exit;
}
}

private function checkSignature()
{
// you must define TOKEN by yourself
if (!defined("TOKEN")) {
throw new Exception('TOKEN is not defined!');
}

$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];

$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
// use SORT_STRING rule
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );

if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}