您的位置:首页 > 移动开发 > 微信开发

PHP 微信公众号 群发文本消息

2018-01-30 16:57 337 查看
//公众号  群发文本消息
//http://api.lialiu.com/test/qunfa
public function qunfa(){
//https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=ACCESS_TOKEN
//引入微信工具类
import("ORG.Util.Gongzhonghao");
//实例化
$wx = new weixin;
#获取token
$token=$wx->get_token();
#文本消息
$msgArray= array(
"touser"=>  array(
"",
),
"msgtype"=>"text",
"text"=>array(
"content"=>urlencode("感谢关注赏金联盟!")
)
);
$json=urldecode(json_encode($msgArray));
//var_dump($json);exit;
#群发 文本消息
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=".$token);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
curl_close($ch);
if($res['errcode']==0){
echo "推送成功";
}else{
echo "推送失败!";
}
}


使用 urlencode 是为了避免在 json_encode的时候被转码unicode.

追加:今天发现用户收不到群发消息,于是又去查了文档,发现文档上有这么一行小字。这意味着,每月只能群发4条消息。

对于认证服务号虽然开发者使用高级群发接口的每日调用限制为100次,但是用户每月只能接收4条,无论在公众平台网站上,还是使用接口群发,用户每月只能接收4条群发消息,多于4条的群发将对该用户发送失败;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: