php 内容回复,php版微信自定义回复功能示例

本文实例讲述了php版微信自定义回复功能。分享给大家供大家参考,具体如下:

8cf4e510e602db803d41e02c65a63697.png

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

index.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 = "

%s

0

";

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;

}

}

}

?>

效果:

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

865a7088d72e433bfdf7cc08ca73eeea.png

希望本文所述对大家PHP程序设计有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值