💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
**目录 (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(); ```