本文实例讲述了php版微信自定义回复功能。分享给大家供大家参考,具体如下:
配置好服务器之后,就可以用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相关内容感兴趣的读者可查看本站专题:《php微信开发技巧汇总》、《php编码与转码操作技巧汇总》、《php网络编程技巧总结》、《php基本语法入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!