PHP利用模板消息无限制向用户推送消息
Posted Champion-水龙果
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP利用模板消息无限制向用户推送消息相关的知识,希望对你有一定的参考价值。
<?php //获取微信access_token function getaccess_token(){ //appid与appsecret改成你自己的 $appid = ‘自己的appid‘; $appsecret = ‘自己的appsecret‘; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0); $data = curl_exec($ch); curl_close($ch); $data = json_decode($data,true); return $data[‘access_token‘]; } //设置与发送模板信息 function set_msg($openid){ //获取access_token $access_token = getaccess_token(); //这里是在模板里修改相应的变量 $formwork = ‘{ "touser":"‘.$openid.‘", "template_id":"oasLSlzdPXF-4U21JRE0lFYLsZWVVFcxY20SC6EAxx4", "url":"http://www.wangwenxiao.com", "data":{ "title": { "value":"这里是自己定义的标题", "color":"#173177" }, "content":{ "value":"这里是自定义内容", "color":"#173177" }, "time": { "value":"这里填写时间", "color":"#173177" } } }‘; $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$access_token}"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0); curl_setopt($ch, CURLOPT_POST,1); curl_setopt($ch, CURLOPT_POSTFIELDS,$formwork); $data = curl_exec($ch); curl_close($ch); return $data; } function sendall(){ //获取access_token $access_token = getaccess_token(); $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token={$access_token}&next_openid="; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0); $data = curl_exec($ch); $data = json_decode($data,true); return $data[‘data‘][‘openid‘]; } //‘{"total":3,"count":3,"data":{"openid":["oC8xks2kb67MogD8ubzkViHf88x4","oC8xksxbsJMf1FKpr4YEfGsqKHtg","oC8xks4COCfqox1-dXTCsZF_1vIo"]},"next_openid":"oC8xks4COCfqox1-dXTCsZF_1vIo"}‘ // array(3) { // [0]=> // string(28) "oC8xks2kb67MogD8ubzkViHf88x4" // [1]=> // string(28) "oC8xksxbsJMf1FKpr4YEfGsqKHtg" // [2]=> // string(28) "oC8xks4COCfqox1-dXTCsZF_1vIo" // } $all_openid = sendall(); //把上面设置的信息循环发送到所有的公众号关注的用户手里 foreach ($all_openid as $value) { set_msg($value); } echo ‘执行完毕‘;
以上是关于PHP利用模板消息无限制向用户推送消息的主要内容,如果未能解决你的问题,请参考以下文章