php 向公众号发送消息,微信公众号之主动给用户发送消息功能

前一段时间项目中遇到一个稍微麻烦一点的问题。

即客户要求,他在后台编辑好文章后要主动给每个用户都发送消息,并可以让用户点击直接进入文章页面。

于是乎,当时脑子一热,想着没什么大的问题,so easy。模板消息不就得了。

后来在写代码的过程中却发现,并不行。

微信公众号中模板消息有很严格的限制。

1.必须有用户主动触发事件方可发送模板消息

2.模板消息一分钟只能发送六十条,超过六十条,不好意思。你懂。

于是乎,就想到了另一种方法:群发消息

但是一看文档中群发消息的限制,发现悲催了

群发消息服务号每个月最多只能发送4条。

而客户要求的是随时随地就能发送.这个4条明显不符合要求的。

怎么办呢?一度陷入困难之中。只好继续查看开发文档。

当看到客服功能时,突然想到,能不能用客服主动给用户发送消息呢?

于是乎,想到就去试验下。

首先,先在公众号后台功能模块中-》添加功能模块  添加   客服功能   模块

如图所示:

然后就进入代码环节了。

先添加客服。也可以公众号后台添加

//先添加客服

function addkf()

{

$token = getToken();

$url = 'https://api.weixin.qq.com/customservice/kfaccount/add?access_token='.$token;

$data = '{

"kf_account" : "system@system",

"nickname" : "客服1",

"password" : "admin",

}';

echo https_request($url,$data);

}

然后就是主动发送消息的方法

//获取access_token的方法。

function getToken()

{

$appid = 'appid';

$appsecret = 'appsecret';

$token_file = dirname(dirname(__FILE__)).'/data/token.txt';

if(!file_exists($token_file) || ((time() - filemtime($token_file)) > 7000)){

$TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;

$json=file_get_contents($TOKEN_URL);

$result=json_decode($json);

$ACC_TOKEN=$result->access_token;

file_put_contents($token_file,$ACC_TOKEN);

}else{

$ACC_TOKEN = file_get_contents($token_file);

}

return $ACC_TOKEN;

}

//调用发送方法

function sendmsg($content,$openid)

{

$token = getToken();

$url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$token;

$content = '感谢你的关注\n回复你厉害 \n例如回复123456';

$data = '{

"touser":"'.$openid.'",

"msgtype":"text",

"text":

{

"content":"'.$content.'"

}

}';

https_request($url,$data);

return true;

}

/**

* request 请求

*/

function https_request($url, $data = null){

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url);

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

if (!empty($data)){

curl_setopt($curl, CURLOPT_POST, 1);

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

}

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$output = curl_exec($curl);

curl_close($curl);

return $output;

}

使用的时候直接调用 sendmsg()方法传值就可以了。

最后上完成的图

如图所示,公众号可以给用户发送消息,而且发送的内容你可以使用html 标签哦

虽然这样感觉很方便,但是同样,微信公众平台对此也有限制。

1.用户必须关注公众号,方可收到信息。

2.用户只能连续收到   20   条客服消息。超过后用户就收不到消息,

超过后,用户必须   主动给公众号发送消息  。或者  点击菜单栏,这样20条消息的限制就会重置。

总结

以上所述是小编给大家介绍的微信公众号之主动给用户发送消息功能,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441685.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【CodeForces - 764A】Taymyr is calling you (找规律,水题)

题干: Comrade Dujikov is busy choosing artists for Timofeys birthday and is recieving calls from Taymyr from Ilia-alpinist. Ilia-alpinist calls every n minutes, i.e. in minutes n, 2n, 3n and so on. Artists come to the comrade every m minutes, …

ecshop php升级,升级-安装与升级- ECShop帮助

ECShop V2.6.2版本有GBK和UFT-8两种编码格式的程序,而低于ECShop V2.6.0 的版本只有UTF-8 编码的(这些版本包括 ECShop 2.5.1、ECShop 2.5.0、ECShop 2.1.5、ECShop 2.1.2b等), 这些版本升级到ECShop V2.6.2均可以使用本程序升级。相同版本的升级只需要覆…

【CodeForces - 764B 】Timofey and cubes (模拟)

题干: Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Every cube has a number ai, which is written on it. Timofey put all the cubes in a row and went to unpack other presents. In this time, Tim…

php页面转发,php如何实现页面路由转发

php实现页面路由转发的方法:首先配置nginx服务器,在【.htaccess】中写上nginx的语法;然后打开根目录的【index.php】,编写文件路由即可。php实现页面路由转发的方法:1、配置nginx服务器nginx服务器不会自动读取.htacce…

【CodeForces - 764D】Timofey and rectangles (四色定理 + 找规律 + 构造)

题干: One of Timofeys birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, bu…

php整站防注入程序,一个不错的php通用防注入程序

代码如下:function jk1986_checksql(){$bad_str "and|select|update||delete|insert|*";$bad_Array explode("|",$bad_str);/** 过滤Get参数 **/foreach ($bad_Array as $bad_a){foreach ($_GET as $g){if (substr_count(strtolower($g),$bad_a) > 0)…

【HDU - 5056】Boring count (尺取法)

题干: You are given a string S consisting of lowercase letters, and your task is counting the number of substring that the number of each lowercase letter in the substring is no more than K. Input In the first line there is an integer T , ind…

java图片上传被旋转,在其他大牛那看到的java手机图片上传旋转问题的解决方法...

// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理//Base64所用包:org.apache.commons.codec.binary.Base64public static String encodeImgageToBase64(File imageFile) {byte[] data null;// 读取图片字节数组try {InputStream in new Fi…

【qduoj】奇数阶幻方 (构造)

题干: C语言_魔方阵 描述 魔方阵是一个古老的智力问题,它要求在一个mm的矩阵中填入1~m2的数字(m为奇数),使得每一行、每一列、每条对角线的累加和都相等,如下为5阶魔方阵示例。 15 8 1 24 17…

【牛客161 - A】字符串(尺取法,桶标记法)

题干: 时间限制:C/C 1秒,其他语言2秒 空间限制:C/C 32768K,其他语言65536K 64bit IO Format: %lld 题目描述 小N现在有一个字符串S。他把这这个字符串的所有子串都挑了出来。一个S的子串T是合法的,当且仅…

mysql innodb 全表锁,Mysql InnoDB行锁及表锁分享

一. 背景知识二. 步入正题:表锁和行锁1.1. 表锁 vs 行锁在 MySQL 中锁的种类有很多,但是最基本的还是表锁和行锁:表锁指的是对一整张表加锁,一般是 DDL 处理时使用,也可以自己在 SQL 中指定;而行锁指的是锁…

【牛客 - 185A】无序组数 (思维,数学,因子个数)

题干: 时间限制:C/C 1秒,其他语言2秒 空间限制:C/C 131072K,其他语言262144K 64bit IO Format: %lld 题目描述 给出一个二元组(A,B) 求出无序二元组(a,b) 使得&#x…

php万能查询用预,PHP 与 mysql

一、php 的 sql 注入攻击1.1、什么是 sql 注入攻击用户提交一段数据库查询代码,根据返回的结果,获得某些他想得到的数据。比如 :查询某个管理员是否存在,一般程序员会这么写$sql "select * from user where nameluluyii and…

php 判断radio选中哪个,jquery如何判断单选按钮radio是否选中

jquery判断单选按钮radio是否选中的方法:1、加载页面的时候获取id,代码为【var fs$("#"id).val()】;2、点击按钮的时候获取id,代码为【var id $(this).attr("id")】。本教程操作环境:windows7系统…

【qduoj】【超级楼梯进阶版】

题干: 描述 N级阶梯,人可以一步走一级,也可以一步走两级,求人从阶梯底端走到顶端可以有多少种不同的走法。 输入 一个整数n,代表台阶的阶数。 输出 求人从阶梯底端走到顶端可以有多少种不同的走法,输出结…

matlab在光学实验中的应用,matlab在光学实验中的应用

matlab在光学实验中的应用 《MATLAB》课程论文MATLAB 在光学实验中的应用姓名:学号:专业:班级:指导老师:学院:完成日期:1MATLAB 在波动光学中的应用(姓名:郑苗苗 12012241736 2012 级…

【HDU - 6016】Count the Sheep (思维,类似二分图)

题干: Altough Skipping the class is happy, the new term still can drive luras anxious which is of course because of the tests! Luras became worried as she wanted to skip the class, as well as to attend the BestCoder and also to prepare for test…

如何生成时间序列matlab,求助:在MATLAB里如何输入时间序列中的时间

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼function[logRS,logERS,V]RSana(x,n,method,q)%Syntax:[logRS,logERS,V]RSana(x,n,method,q)%____________________________________________%% Performs R/Sanalysis on a time series.%% logRS is thelog(R/S).% logERS is theEx…

【CF#192 A】Funky Numbers (二分,查找,二元组)

题干: As you very well know, this years funkiest numbers are so called triangular numbers (that is, integers that are representable as , where k is some positive integer), and the coolest numbers are those that are representable as a sum of two…

matlab考试试题,matlab-考试试题-

matlab-考试试题- MATLAB 考试试题 (1) 产生一个1x10的随机矩阵,大小位于( -5 5),并且按照从大到小的顺序排列好!(注:要程序和运行结果的截屏)答案:a10*rand(1,10)-5;bsort(a, descend )1.请产生一个100*5 的矩阵&…