**目录 (Table of Contents)**
[TOC]
## 微信关注/取消关注消息示例
```PHP
$receiver = new Receiver();
$subscribeMsg = "欢迎关注我的公众号";
// 监听一个关注事件,在客户端用户关注微信公众号的时候,响应subscribe事件
$receiver ->addEventListener(Receiver::EVENT_SUBSCRIBE, function(/*SubscribeEventMessage*/ $msg) use($subscribeMsg) {
// 返回文本消息(Text类型)给客户端
$textMessage = new TextMessage($msg->getPlatform(),$msg->getUser(), time(), null, $subscribeMsg);
$textMessage->response();
});
// 监听一个取消关注事件,在客户端用户取消关注微信公众号的时候,响应unsubscribe事件
$receiver ->addEventListener(Receiver::EVENT_UNSUBSCRIBE, function(/*UnSubscribeEventMessage*/ $msg) {
// 这里可以操作数据库,删除用户信息等,或者记录用户已经取消关注
file_put_contents("log.txt","用户:". $msg->getUser()."于".date("Y-m-d H:i:s")."取消公众号关注\r\n",FILE_APPEND);
// do something here
});
$receiver ->receiver();
```
## 微信点击事件消息示例
```PHP
// 假设我们有一个微信click事件的菜单,并且将该菜单的Key设置为 LVS_TEST1
$receiver = new Receiver();
// 我们在这里回复文本消息
$receiver->addEventListener(Receiver::EVENT_CLICK, function(ClickEventMessage $msg) {
$responseText = "您刚刚点击了事件为". $msg->getEventKey()."事件的Button";
switch($msg->getEventKey()) {
case "LVS_TEST1":
$textMessage = new TextMessage($msg->getPlatform(),$msg->getUser(), time(), null, $responseText);
$textMessage->response();
break;
}
});
$receiver->receiver();
```
## 微信回复图文消息
```JSON
假设我们从数据库中获取出来的图文资源如下所示格式:
$resultsArr = [
[
"picture" => "https://endor-text/1.png",
"remarks"=>"这里是新闻1的文章简介",
"title"=>"新闻1",
"articeid"=>"11",
],
[
"picture" => "https://endor-text/1.png",
"remarks"=>"这里是新闻2的文章简介",
"title"=>"新闻2",
"articeid"=>"12",
],
[
"picture" => "https://endor-text/3.png",
"remarks"=>"这里是新闻3的文章简介",
"title"=>"新闻3",
"articeid"=>"13",
],
];
```
```PHP
// 假设我们有一个LVS_TEST2的Button
$receiver = new Receiver();
$receiver->addEventListener(Receiver::EVENT_CLICK, function(/*ClickEventMessage*/ $msg) {
switch($msg->getEventKey()) {
case "LVS_TEST1":
// to do something
break;
case "LVS_TEST2":
// 您需要在这里从数据库获取资源,我们假设已经从数据库中获取到$resultsArr
$newsMessage = new NewsMessage($msg->getPlatform(),$msg->getUser(), time(), null, $responseText);
try {
foreach($resultsArr as $v)
{
// 跳转URL地址
$link = "https://endor-test/article/".$v['articleid'].".html";
// 向图文消息列表对象中循环插入图文消息上下文结构
$newsMessage->addNewsContext(new NewsContext($v['title'],$v['remarks'],$v['picture'], $link));
}
$newsMessage->response();
}
catch(WechatException $e)
{
echo $e->errorMessage();
}
break;
}
});
$receiver->receiver();
```
## Echo 服务 (客户端微信留言处理)
```PHP
// 该例子展示用户在微信客户端交流区输入文字,服务器的事件响应
$receiver = new Receiver();
$receiver->addEventListener(Receiver::TEXT, function(TextMessage $msg) {
echo "接收到客户端发来的文本消息内容为:" . $msg->getContent();
if($msg->getContent() == "时间")
{
$echoStr = "当前时间是:".Date("Y-m-d H:i:s");
$textMessage = new TextMessage($msg->getPlatform(),$msg->getUser(), time(), null,$echoStr);
$textMessage->response();
}
});
$receiver->receiver();
```
- 1、序言
- 1.1、更新说明
- 1.2、更新计划
- 2、开始使用 Endor
- 3、目录结构
- 4、类说明
- 4.1、WechatConfig
- 4.2、WechatValid
- 4.3、WechatCommon
- 4.4、WechatException
- 4.5、基础接口
- 4.5.1、ShortUrl
- 4.5.2、WechatServerApi
- 4.6、菜单接口
- 4.6.1、菜单类设计
- 4.6.2、Menu
- 4.6.3、ClickMenu
- 4.6.4、ViewMenu
- 4.6.5、MiniProgramMenu
- 4.6.6、MenuApi
- 4.7、消息接口
- 4.7.1、Receiver
- 4.7.2、Message
- 4.7.3、EventMessage
- 4.7.4、TextMessage
- 4.7.5、ClickEventMessage
- 4.7.6、SubscribeEventMessage
- 4.7.7、UnSubscribeEventMessage
- 4.7.8、LocationEventMessage
- 4.7.9、ImageMessage
- 4.7.10、LinkMessage
- 4.7.11、NewsMessage
- 4.7.12、NewsContext
- 4.8、OAuth2.0权限验证
- 4.8.1、WechatUser
- 4.8.2、OAuthApi
- 4.9、微信支付接口
- 4.9.1、微信支付类设计
- 4.9.2、PayApi
- 4.9.3、RequestPay
- 4.9.4、ResponsePay
- 4.9.5、UnifiedOrder
- 4.9.6、JsPay
- 4.9.7、Refund
- 4.9.8、Query
- 4.9.9、Bill
- 4.9.10、NotifyData
- 4.9.11、Notify
- 4.10、模板消息接口
- 4.10.1、TemplateContext
- 4.10.2、TemplateMessageApi
- 5、使用场景
- 5.1、微信接入服务器验证
- 5.2、获取微信API令牌(access_token)
- 5.3、获取JS API
- 5.4、OAuth2.0静默登录
- 5.5、微信菜单
- 5.6、微信消息
- 5.7、微信支付
- 5.7.1、微信公众号内JsApi支付
- 5.7.2、Native 模式二 扫码支付
- 5.8、模板消息发送