具体如下:
配置好服务器之后,就可以用php实现自动回复了。
index.php中的代码
<?php 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()){ header('content-type:text'); echo $echostr; exit; } } private function checksignature() { $signature = $_get["signature"]; $timestamp = $_get["timestamp"]; $nonce = $_get["nonce"]; $token = token; $tmparr = array($token, $timestamp, $nonce); sort($tmparr, sort_string); $tmpstr = implode( $tmparr ); $tmpstr = sha1( $tmpstr ); if( $tmpstr == $signature ){ return true; }else{ return false; } } public function responsemsg() { $poststr = $globals["http_raw_post_data"]; 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>"; if($keyword == "?" || $keyword == "?") //获取用户信息 { $msgtype = "text"; $contentstr = date("y-m-d h:i:s",time()); // 回复的内容 $resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr); echo $resultstr; } }else{ echo ""; exit; } } } ?>
效果:
当用户输入?或者?就会获取当前时间
以上就是本文的全部内容,希望对大家的学习有所帮助。
相关推荐:
php mysql 简介(数据库的相关知识)
php mysql 读取数据的操作及方法
php 发送电子邮件的使用方法
以上就是php版微信自定义回复功能详解的详细内容。
