PHP实现微信公众号自动回复、获取用户消息Demo代码

admin · 2024-05-18 09:49:22



这是一份php实现微信公众号自动回复的演示代码,提供了文字回复、图文回复的demo,其他如声音、链接,关注事件等均没有,此代码只是方便接入微信公众号进行开发测试,尤其是哪一步配置接口和token报错,只是更好的帮助和步入开发,更深入的功能还需自行研究。



代码基于thinkphp5.0测试,测试无误,下面仅部分代码展示,如需完整代码请付费后在付费内容中查看。



{
// 接口
public function wx_msg()
{
$this->valid(); // 验证消息
$this->responseMsg(); // 处理消息
}

// 微信验证
public function valid()
{

}

// 处理微信消息
public function responseMsg()
{
// 获取微信推送的数据
$postStr = file_get_contents("php://input");

if (!empty($postStr)) {

libxml_disable_entity_loader(true);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);

// 消息类型
$msgType = trim($postObj->MsgType);

// 根据消息类型进行处理
switch ($msgType) {
case 'text':
$this->receiveText($postObj);
break;
// 其他消息类型处理
case 'image':
// $this->replyText($postObj, "image type");
$this->receiveImage($postObj);
break;
default:
// echo "other info";
$this->replyText($postObj, "other type msg");
break;
}
} else {
echo "empty";
exit;
}
}

// 检查微信签名
private function checkSignature()
{
$signature = isset($_GET["signature"]) ? $_GET["signature"] : '';
$timestamp = isset($_GET["timestamp"]) ? $_GET["timestamp"] : '';
$nonce = isset($_GET["nonce"]) ? $_GET["nonce"] : '';

$token = TOKEN;
// 填写你在微信公众号后台设置的TOKEN
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);

return $tmpStr === $signature;
}

// 接收文本消息
private function receiveText($postObj)
{
// 获取用户发送的文本内容
$content = trim($postObj->Content);

// 判断是否是纯数字且小于8位数
// 判断用户消息进行回复
if($content=='你好'){
$this->replyText($postObj, '他好你好大家好');
}

return $this->replyText($postObj, '收到您的消息:' . $content);
}

// 回复文本消息
private function replyText($postObj, $content)
{
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$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>";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $content);
echo $resultStr;
}


// 接收图片消息
private function receiveImage($postObj)
{
// 回复图文消息
$this->replyImage($postObj);
}

// 回复图片消息
private function replyImage($postObj)
{
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$time = time();
$msgType = 'image';

// 图片素材ID
$media_id = "GHYb4PhayrbCAWgHCwkBQIAg_b46UI0Dl-xJukV-XoryFMp-t5c50_VqyId074_U";

// 图文消息XML模板
$newsTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>1</ArticleCount>
<Articles>
<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
</Articles>
</xml>";
echo sprintf($newsTpl,$fromUsername,$toUsername,$time,"news","这是一篇图文卡片消息","只需向公众号发送一条消息即可生成卡片!","https://d1.faiusr.com/2/AAEIABACGAAg_vuznQYogr-b2gIwuAg4uAg.jpg%22,%22https://www.yunupan.cn%22);
}

}

由于编辑器问题,代码需自行格式化~



167 

微信公众号,PHP开发,自动回复,消息获取,测试开发

文件下载
download background

「下载版权声明」 本内容由admin 在 2024-05-18 09:49:22 转载 ,来源 网络。
[由发布者设置,价格可能会涨价、降价,请以当前为准]
此内容须付费,价值: ¥0.5 , 仅支持 【支付宝】 平台,其它支付联系客服转换。

账号登录 免费注册
优优助手

admin

这个人太懒咯