php发送订阅消息

<?php

namespace app\api\controller;
use think\Controller;
use think\Request;

/**
 * 订阅消息推送
 */
class Send extends Controller
{
    protected $appid;
    protected $appSecret;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->appid = '********'; //你的小程序appid
        $this->appSecret = '*********'; //小程序密钥

    }
    // 发送 POST 请求的函数
    function send_post($url, $post_data){
        $options = array(
            'http' => array(
                'method'  => 'POST',

                'header'  => 'Content-type:application/json',
                'content' => $post_data,
                'timeout' => 60
            )
        );
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        return $result;
    }

    // 小程序 appID 和 appSecret 获取 token
    public function getAccessToken($appid,$appsecret){
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsecret;
        $html = file_get_contents($url);
        $output = json_decode($html, true);
        $access_token = $output['access_token'];

        return $access_token;
    }

     /**
     *open_id用户的open_id
     *template_id模板的id
     *$data内容
     *page需要跳转的小程序的页面
     */
    public function send($open_id,$template_id,$data,$page){

        $post_data = array(
            // 用户的 openID,可用过 wx.getUserInfo 获取
            "touser"           => $open_id,
            // 小程序后台申添加的订阅消息模板 ID
            "template_id"      => $template_id,
            // 点击模板消息后跳转到的页面,可以传递参数
             "page"             => $page,
            // 发送给用户的数据
            "data"             => $data
        );
//        file_put_contents('dingyue2.txt',date('Y-m-d H:i:s').json_encode($post_data, true).PHP_EOL, FILE_APPEND);
        $appid=$this->appid;
        $appsecret=$this->appSecret;
        //     // 这里替换为你的 appID 和 appSecret
        $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=".$this->getAccessToken($appid,$appsecret);
        $data = json_encode($post_data, true);
        $return = $this->send_post($url, $data);
//      下方是在根目录下面生成一个文本记录发送的状态
//        file_put_contents('dingyue.txt',json_encode($return, true).PHP_EOL, FILE_APPEND);
    }

}

发送的类到这里就结束了

这里开始调用
    /**直播主题{{thing1.DATA}}  开播时间 {{time2.DATA}}
    温馨提示 {{thing3.DATA}}
    主播名称 {{thing4.DATA}}
     */
    public function send($title,$time,$desc,$user_name,$open_id,$id){
        /****************推送消息************************/

        $send = new Send();
        //data内容根据自己的需要去小程序·后台看,此处我i用的是开播提醒
        $data = [
            'thing1'=>['value'=>mb_substr($title,0,10)],//主题
            'time2'=> ['value'=>$time],//时间
            'thing3'=>['value'=>$desc],//提示
            'thing4'=>['value'=>$user_name],//主播名称
        ];
        $page = 'pagesc/livePlayer/live-player?id='.$id;
        //此处的参数·对应上面的发送类里面的顺序
        $send->send($open_id,'g9f1JXx0kxMcX6WTHAw4HPTvePqQLM1i1ZEliZi1l44',$data,$page);
        /*******************推送消息*********************/
    }

到此就结束,我是用thinkphp5.0,此方法直接复制出去即可使用,但注意小程序中一定要同意订阅消息,否则接受不到消息

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值