- 实现微信接入
- 实现功能 当用户关注时 回复文本消息你好
- 当用户在公众号中回复内容时,回复图片消息。
- 当用户在公众号发送固定内容(只要内容中存在指定内容即可,未必全匹配)时,回复图文消息。
扩展:当用户发送指定内容时,随机发送一种类型消息
演示图
共4个文件 index.php config.php Message.php WeChat.php
index.php 入口文件
<?php
//封装代码调用
include 'config.php';
include 'WeChat.php';
include 'Message.php';
$obj = new Message();
$obj->postText();
config.php 定义TOKEY
<?php
define( 'TOKEN', 'yuanyiruciphp' );
WeChat.php 与微信接入文件
<?php
class WeChat
{
private $token = TOKEN;
public function checkSignature()
{
$signature = $_GET['signature'];
$timestamp = $_GET['timestamp'];
$nonce = $_GET['nonce'];
$echostr = $_GET['echostr'];
$tmpArr = array( $this->token, $timestamp, $nonce );
sort( $tmpArr, SORT_STRING );
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
//进行比对
if ( $tmpStr == $signature ) {
echo $echostr;
} else {
echo false;
}
}
}
Message.php 消息会话管理文件
<?php
class Message extends WeChat
{
function postText()
{
if ( $_SERVER['REQUEST_METHOD'] == 'GET' ) {
//开发者服务器验证
$this->checkSignature();
} elseif ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
//接收
$xml = file_get_contents( 'php://input' );
if ( !empty( $xml ) ) {
$obj = simplexml_load_string( $xml, 'SimpleXMLElement', LIBXML_NOCDATA );
$toUser = $obj->FromUserName;
$formUser = $obj->ToUserName;
$time = time();
//如果是关注
if ( strtolower( $obj->MsgType ) == 'event' ) {
if ( strtolower( $obj->Event ) == 'subscribe' ) {
$msgType = 'text';
$content = '你好,感谢你的关注';
//回复用户关注的相关信息
$this->text( $toUser, $formUser, $time, $msgType, $content );
}
}
//如果发送文本消息
if ( strtolower( $obj->MsgType ) == 'text' && preg_match( '/你好/S', $obj->Content ) ) {
$title = '带有你好内容的回复';
$description = '带有你好内容的回复内容介绍带有你好内容的回复内容介绍';
$picUrl = 'https://www.php.cn/static/images/logo1.png';
$url = 'https://www.php.cn/';
$this->imgText( $toUser, $formUser, $time, 1, $title, $description, $picUrl, $url );
} elseif ( strtolower( $obj->MsgType ) == 'text' && preg_match( '/开心/S', $obj->Content ) ) {
$msgType = 'text';
$content = '带有开心的文本回复';
$this->text( $toUser, $formUser, $time, $msgType, $content );
} else {
$title = '任意消息回复';
$description = '任意消息回复内容介绍任意消息回复内容介绍';
$picUrl = 'https://img.php.cn/upload/course/000/000/001/60b5cd439a6b7169.png';
$url = 'https://www.php.cn/';
$this->imgText( $toUser, $formUser, $time, 1, $title, $description, $picUrl, $url );
}
}
}
}
//回复文本消息
protected function text( $toUser, $formUser, $time, $msgtype, $content )
{
$templade = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>
";
$info = sprintf( $templade, $toUser, $formUser, $time, $msgtype, $content );
echo $info;
}
//回复图文消息
protected function imgText( $toUser, $formUser, $time, $num, $title, $description, $picurl, $url )
{
$templade = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>%s</ArticleCount>
<Articles>
<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
</Articles>
</xml>";
$info = sprintf( $templade, $toUser, $formUser, $time, $num, $title, $description, $picurl, $url );
echo $info;
}
}