PHP 操控微信公众号
<?php
class AutoAction extends CommonAction
{
public function index()
{
$timestamp = $_GET['timestamp'];//timestamp其实就是一个时间戳
$nonce = $_GET['nonce'];//nonce是一个随机参数
$token = "ATA2zJ3969DD99SlJKYJhy22j96GKKlB";//这个token填写你在微信公众平台上写的那个值
$signature = $_GET['signature'];//这个signature其实就是在微信公众平台已经加密好的字符串
$echostr = $_GET['echostr'];
$array = array($timestamp, $nonce, $token);
sort($array);
$tmpstr = implode('', $array);
$tmpstr = sha1($tmpstr);
if ($tmpstr == $signature && $echostr) {
echo $echostr;
exit;
} else {
$this->response_msg();
}
}
private function response_msg()
{
$postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
$postObj = simplexml_load_string($postArr);
if (strtolower($postObj->MsgType) == 'event') {
//回复用户消息
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
//如果是关注事件(subscribe)
if (strtolower($postObj->Event == 'subscribe')) {
// 是否有推荐人
$referrer = $postObj->EventKey;
// 关注人openid
$openid = $toUser->__toString();
$referrer_str = $referrer->__toString();
if ($referrer_str) {
$referrer_openid = explode('_',$referrer_str)[1];
// 判断关注人是否已关注别人
$wx_recommend = M('wx_recommend');
$exist = $wx_recommend->where(['new_openid'=>$openid])->find();
if (!$exist) { // 有效的用户
// 添加记录
$data = [
'openid' => $referrer_openid,
'new_openid' => $openid,
'add_time'=> time(),
];
$wx_recommend->add($data);
}
}
$arr = array(
array(
'title' => '标题',
'description' => "描述",
'picUrl' => 'xxx.jpg',
'url' => 'xxx.html',
),
);
$this->_send_news($arr,$toUser,$fromUser);
} elseif ($postObj->Event == 'CLICK') {
//------------------- 点击事件 start ------------------------
$event_key = $postObj->EventKey; // 获取key
if($event_key=='V1001_PRESENT'){
$arr = array(
array(
'title' => '标题',
'description' => "描述",
'picUrl' => 'xxx.jpg',
'url' => 'xxx.html',
),
);
$this->_send_news($arr,$toUser,$fromUser);
}
//------------------- 点击事件 end ------------------------
}
}
//回复纯文本或单图文消息
if (($postObj->MsgType) == 'text' && (trim($postObj->Content) == '我要帽子' || trim($postObj->Content) == '帽子')) {
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$arr = array(
array(
'title' => '标题',
'description' => "描述",
'picUrl' => 'xxx.jpg',
'url' => 'xxx.html',
),
);
$this->_send_news($arr,$toUser,$fromUser);
} else if(($postObj->MsgType) == 'text' && (trim($postObj->Content) == '数量' || trim($postObj->Content) == '6')) {
$fromUser = $postObj->ToUserName;//消息从哪里来
$toUser = $postObj->FromUserName;//发送给谁
// 用户openid
$openid = $toUser->__toString();
// 获取推荐数量
$wx_recommend = M('wx_recommend');
$count = $wx_recommend->where(['openid'=>$openid])->count();
$content = "您当前已推荐关注人数为:".$count;
$this->_send_text($content,$toUser,$fromUser);
} else {
$fromUser = $postObj->ToUserName;//消息从哪里来
$toUser = $postObj->FromUserName;//发送给谁
$content = "内容";
$this->_send_text($content,$toUser,$fromUser);
}
}
private function _send_text($content,$toUser,$fromUser) {
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$time = time();
$msgType = 'text';
echo sprintf($template, $toUser, $fromUser, $time, $msgType, $content);
}
private function _send_news($arr,$toUser,$fromUser) {
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>" . count($arr) . "</ArticleCount>
<Articles>";
foreach ($arr as $k => $v) {
$template .= "<item>
<Title><![CDATA[" . $v['title'] . "]]></Title>
<Description><![CDATA[" . $v['description'] . "]]></Description>
<PicUrl><![CDATA[" . $v['picUrl'] . "]]></PicUrl>
<Url><![CDATA[" . $v['url'] . "]]></Url>
</item>";
}
$template .= "</Articles>
</xml> ";
$time = time();
$msgType = "news";
echo sprintf($template, $toUser, $fromUser, $time, $msgType);
}
}
PHP 操控微信公众号的更多相关文章
- 使用flask搭建微信公众号:实现签到功能
终于到了实战阶段.用微信公众号实现一个简单的签到功能. 前情提要: 微信公众号token验证失败 使用flask搭建微信公众号:完成token的验证 使用flask搭建微信公众号:接收与回复消息 程序 ...
- Reinforcement Learning,微信公众号:DRL学习
欢迎大家关注微信公众号:DRL学习,我们一起来学习强化学习和深度强化学习的算法及现状应用问题. 强化学习简单说就是学习如何最大化未来奖励的预期总和,以及agent学会在环境中做出的行动序列,其中随机状 ...
- 微信公众号开发之VS远程调试
目录 (一)微信公众号开发之VS远程调试 (二)微信公众号开发之基础梳理 (三)微信公众号开发之自动消息回复和自定义菜单 前言 微信公众平台消息接口的工作原理大概可以这样理解:从用户端到公众号端一个流 ...
- 快递Api接口 & 微信公众号开发流程
之前的文章,已经分析过快递Api接口可能被使用的需求及场景:今天呢,简单给大家介绍一下微信公众号中怎么来使用快递Api接口,来完成我们的需求和业务场景. 开发语言:Nodejs,其中用到了Neo4j图 ...
- 微信公众号开发(一)--验证服务器地址的Java实现
现在主流上都用php写微信公众号后台,其实作为后端语言之一的java也可以实现. 这篇文章将对验证服务器地址这一步做出实现. 参考资料:1.慕课网-<初识java微信公众号开发>,2.微信 ...
- NodeJs 开发微信公众号(五)真实环境部署
在测试环境下开发完成代表着你离正式上线的目标不远了.接下来本章就主要谈一谈把测试环境的公众号升级为正式的公众号. 服务器和域名 目前为止我们只是在自己的电脑上完成了测试环境.真实的线上环境当然需要自己 ...
- NodeJs 开发微信公众号(四)微信网页授权
微信的网页授权指的是在微信公众号中访问第三方网页时获取用户地理.个人等信息的权限.对于开发了自己的网页app应用时,获取个人的信息非常重要.上篇博客讲到了注册时可以获取用户的信息,很多人会问为什么还需 ...
- NodeJs 开发微信公众号(三)微信事件交互
微信公众号有个规则,一旦开启了开发者模式,其他的常规功能就都必须通过接口调用完成.比如说自定义菜单功能,必须通过发送post请求的方式生成.本章就通过关注到取消关注的整个过程来谈一谈nodejs是怎么 ...
- 用java开发微信公众号:接收和被动回复普通消息(三)
上篇说完了如何接入微信公众号,本文说一下微信公众号的最基本功能:普通消息的接收和回复.说到普通消息,那么什么是微信公众号所定义的普通消息呢,微信开发者文档中提到的接收的普通消息包括如下几类: 1.文本 ...
随机推荐
- (0.2.2)如何下载mysql数据库(二进制、RPM、源码、YUM源)
目录 1.基于Linux平台的Mysql项目场景介绍 2.mysql数据库运行环境准备-最优配置 3.如何下载mysql数据库 3.1. 二进制文件包 3.2.RPM文件 3.3.源码包 3.4.yu ...
- 详解C++中命名空间的意义和用法
看过鸡啄米的C++编程入门系列教程的朋友,应该能注意到,在其中的很多实例中,都有这么一条语句:using namespace std;,即使用命名空间std,其作用就是规定该文件中使用的标准库函数都是 ...
- python学习笔记(九)函数返回多个值,列表生成式,循环多个变量,入参格式声明
一.函数返回多个值 1.函数如果返回多个值的话,它会把这几个值放到一个元组里面2.函数如果返回多个值的话,也可以用多个变量来接收 def say(): num1 = num2 = num3 = ret ...
- POJ3903Stock Exchange&&POJ1631Bridging signals最长上升子序列 &&POJ1887Testing the CATCHER(最长下降子序列)(LIS模版题)
题目链接:http://poj.org/problem?id=3903 题目链接:http://poj.org/problem?id=1631 题目链接:http://poj.org/problem? ...
- Zen cart 根据数量打折插件
Quantity Discounts 可以根据顾客购买多少来打折,很不错. 假如顾客买了3个以上的产品,就给他10%折扣,设置如下: Turn On Quantity Discount 1. In t ...
- 关于cgi、FastCGI、php-fpm、php-cgi(复制)
首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. web server(比如说nginx)只是内容的分发者.比如,如果请求/index.h ...
- DOM—节点
节点的相关属性 1.nodeType:节点类型.返回的是一个数字,这个数字代表节点类型,只读. 节点类型: 1 — 元素类型:元素节点(标签) 节点类型: 2 — 元素类型:属性节点 节点类型: 3 ...
- 【android】来电悬浮窗
先看下效果图 说下思路: 1:监听来电广播 2:根据来电号码,和本地数据库做匹配,有记录的,则提取出头像.名字.职位,生成悬浮窗 3:监听来电广播,如果当前行为是空闲的(没有任何通话行为),则删除掉悬 ...
- Tasks in parallel
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- Eclipase .自动提示
打开eclipse→Windows→Preferences→Java→Editor→Content Assist 修改Auto Activation triggers for java的值为 .abc ...