微信公众号接入与发送接收消息
16lz
2021-09-06
微信公众号接入与发送接收消息
作业标题:0830作业
作业内容:1. 实现微信接入 2. 实现功能 当用户关注时 回复文本消息你好 3. 当用户在公众号中回复内容时,回复图片消息。 4. 当用户在公众号发送固定内容(只要内容中存在指定内容即可,未必全匹配)时,回复图文消息。 扩展:当用户发送指定内容时,随机发送一种类型消息
- 实现微信接入
- 实现功能 当用户关注时 回复文本消息你好
代码
<?php
class Message extends WeChat
{
function postText()
{
//判断微信服务请求的方法 get 还是 post
if($_SERVER['REQUEST_METHOD'] == 'GET'){
// //接入验证
$this->checkSignature();
}elseif($_SERVER['REQUEST_METHOD'] == 'POST'){
//读取数据
$xml = file_get_contents('php://input');
if(!empty($xml)){
//将xml数据转换成对象
$postObj = simplexml_load_string($xml,'SimpleXMLElement',LIBXML_NOCDATA);
$toUser = $postObj->FromUserName;
$formUser = $postObj->ToUserName;
$time = time();
//判断用户传入的类型 (文本 图文)
if($postObj->MsgType == 'text' && preg_match('/\d/S',$postObj->Content)){
$title = '真正成功了才是成功';
$description = '进入php.cn主页';
$picurl = 'https://www.php.cn/static/images/index_php_new4.jpg?1';
$url = 'https://www.php.cn';
$this->imgText($toUser,$formUser,$time,$title,$description,$picurl,$url);
}elseif($postObj->MsgType == 'event'){
if($postObj->Event == 'subscribe'){
//制作对应的回复内容
$content = '你好';
$this->text($toUser,$formUser,$time,$content);
}
}
}
}
}
/*
* 制作文本消息
*/
private function text($toUser,$formUser,$time,$content)
{
//制作对应的回复内容
$templade = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
echo sprintf($templade,$toUser,$formUser,$time,$content);
}
/*
* 制作图文
*/
private function imgText($toUser,$fromUser,$time,$title,$description,$picurl,$url)
{
$str="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></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($str,$toUser,$fromUser,$time,$title,$description,$picurl,$url);
}
}
3.当用户在公众号中回复内容时,回复图片消息。
代码
<?php
class Message extends WeChat
{
function postText()
{
//判断微信服务请求的方法 get 还是 post
if($_SERVER['REQUEST_METHOD'] == 'GET'){
// //接入验证
$this->checkSignature();
}elseif($_SERVER['REQUEST_METHOD'] == 'POST'){
//读取数据
$xml = file_get_contents('php://input');
if(!empty($xml)){
//将xml数据转换成对象
$postObj = simplexml_load_string($xml,'SimpleXMLElement',LIBXML_NOCDATA);
$toUser = $postObj->FromUserName;
$formUser = $postObj->ToUserName;
$time = time();
//判断用户传入的类型 (文本 图文)
if($postObj->MsgType == 'text' && preg_match('/\d/S',$postObj->Content)){
$title = '真正成功了才是成功';
$description = '进入php.cn主页';
$picurl = 'https://www.php.cn/static/images/index_php_new4.jpg?1';
$url = 'https://www.php.cn';
$this->imgText($toUser,$formUser,$time,$title,$description,$picurl,$url);
}elseif($postObj->MsgType == 'event'){
if($postObj->Event == 'subscribe'){
//制作对应的回复内容
$content = '你好';
$this->text($toUser,$formUser,$time,$content);
}
}
}
}
}
/*
* 制作文本消息
*/
private function text($toUser,$formUser,$time,$content)
{
//制作对应的回复内容
$templade = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
echo sprintf($templade,$toUser,$formUser,$time,$content);
}
/*
* 制作图文
*/
private function imgText($toUser,$fromUser,$time,$title,$description,$picurl,$url)
{
$str="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></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($str,$toUser,$fromUser,$time,$title,$description,$picurl,$url);
}
}
当用户在公众号发送固定内容(只要内容中存在指定内容即可,未必全匹配)时,回复图文消息。 扩展:当用户发送指定内容时,随机发送一种类型消息
<?php
class Message extends WeChat
{
function postText()
{
//判断微信服务请求的方法 get 还是 post
if($_SERVER['REQUEST_METHOD'] == 'GET'){
// //接入验证
$this->checkSignature();
}elseif($_SERVER['REQUEST_METHOD'] == 'POST'){
//读取数据
$xml = file_get_contents('php://input');
if(!empty($xml)){
//将xml数据转换成对象
$postObj = simplexml_load_string($xml,'SimpleXMLElement',LIBXML_NOCDATA);
$toUser = $postObj->FromUserName;
$formUser = $postObj->ToUserName;
$time = time();
//判断用户传入的类型 (文本 图文)
if($postObj->MsgType == 'text' && preg_match('/\d/S',$postObj->Content)){
$title = '真正成功了才是成功';
$description = '进入php.cn主页';
$picurl = 'https://www.php.cn/static/images/index_php_new4.jpg?1';
$url = 'https://www.php.cn';
$this->imgText($toUser,$formUser,$time,$title,$description,$picurl,$url);
}elseif($postObj->MsgType == 'event'){
if($postObj->Event == 'subscribe'){
//制作对应的回复内容
$content = '你好';
$this->text($toUser,$formUser,$time,$content);
}
}
}
}
}
/*
* 制作文本消息
*/
private function text($toUser,$formUser,$time,$content)
{
//制作对应的回复内容
$templade = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
echo sprintf($templade,$toUser,$formUser,$time,$content);
}
/*
* 制作图文
*/
private function imgText($toUser,$fromUser,$time,$title,$description,$picurl,$url)
{
$str="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></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($str,$toUser,$fromUser,$time,$title,$description,$picurl,$url);
}
}
匹配数字回复图文消息
更多相关文章
- easywechat实现微信接入并不同消息回复+根据项目提供数据字典,对
- 实战原生微信接入和消息回复
- 实现原生微信接入 当用户关注/发送消息时对应内容回应
- 使用easywechat完成微信接入,并且实现对接收不同消息的回复
- 作业内容:oop基础:请举例实例演绎以下难点 1. 类(对象抽象化的结
- CSS_0702作业
- VSCODE的安装配置与插件安装及HTTP协议初识
- HTML入门笔记1
- javascript内容整理