码迷,mamicode.com
首页 > Web开发 > 详细

PHP利用模板消息无限制向用户推送消息

时间:2018-08-27 18:39:20      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:function   www.   推送   etop   api   each   sage   ESS   get   

<?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利用模板消息无限制向用户推送消息

标签:function   www.   推送   etop   api   each   sage   ESS   get   

原文地址:https://www.cnblogs.com/qhorse/p/9542788.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!