调用SAE的数据库,请使用SAE已定义好的常量: MySQL用户名 SAE_MYSQL_USER MySQL密码 SAE_MYSQL_PASS MySQL主库域名 SAE_MYSQL_HOST_M MySQL从库域名 SAE_MYSQL_HOST_S MySQL端口 SAE_MYSQL_PORT MySQL数据库名 SAE_MYSQL_DB简单的文本回复代码如下(别忘了把TOKEN换成自己设置的token),参考博客:http://blog.csdn.net/misakaqunianxiatian/article/details/49401759header('content-type:text/html;charset=utf-8');define("TOKEN", "my_weixin"); //define your token $wx = new wechatCallbackapiTest();if($_GET['echostr']){ $wx->valid(); //如果发来了echostr则进行验证 }else{ $wx->responseMsg(); //如果没有echostr,则返回消息 } class wechatCallbackapiTest{ public function valid(){ //valid signature , option $echoStr = $_GET["echostr"]; if($this->checkSignature()){ //调用验证字段 echo $echoStr; exit; } } public function responseMsg(){
//get post data, May be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //接收微信发来的XML数据 //extract post data if(!empty($postStr)){
MySQL用户名 SAE_MYSQL_USER
MySQL密码 SAE_MYSQL_PASS
MySQL主库域名 SAE_MYSQL_HOST_M
MySQL从库域名 SAE_MYSQL_HOST_S
MySQL端口 SAE_MYSQL_PORT
MySQL数据库名 SAE_MYSQL_DB简单的文本回复代码如下(别忘了把TOKEN换成自己设置的token),参考博客:http://blog.csdn.net/misakaqunianxiatian/article/details/49401759header('content-type:text/html;charset=utf-8');define("TOKEN", "my_weixin"); //define your token
$wx = new wechatCallbackapiTest();if($_GET['echostr']){
$wx->valid(); //如果发来了echostr则进行验证
}else{
$wx->responseMsg(); //如果没有echostr,则返回消息
}
class wechatCallbackapiTest{ public function valid(){ //valid signature , option $echoStr = $_GET["echostr"];
if($this->checkSignature()){ //调用验证字段
echo $echoStr;
exit;
}
} public function responseMsg(){
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //接收微信发来的XML数据 //extract post data
if(!empty($postStr)){
//解析post来的XML为一个对象$postObj
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName; //请求消息的用户
$toUsername = $postObj->ToUserName; //"我"的公众号id
$keyword = trim($postObj->Content); //消息内容
$time = time(); //时间戳
$msgtype = 'text'; //消息类型:文本
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>"; if($keyword == 'hehe'){
$contentStr = 'hello world!!!';
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgtype, $contentStr);
echo $resultStr;
exit();
}else{
$contentStr = '输入hehe试试';
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgtype, $contentStr);
echo $resultStr;
exit();
} }else {
echo "";
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;
}
}
}