微信自定义回复

编程入门 行业动态 更新时间:2024-10-05 21:18:47

微信<a href=https://www.elefans.com/category/jswz/34/1771438.html style=自定义回复"/>

微信自定义回复

配置好服务器之后,就可以用php实现自动回复了。

index.php中的代码

<?phpdefine("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;}}
}
?>

效果:

当用户输入?或者?就会获取当前时间

更多推荐

微信自定义回复

本文发布于:2024-02-28 00:26:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1767266.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!