小程序统一服务消息接口

地址:https://www.cnblogs.com/xinweiyun/p/9634147.html  

下载地址:https://download.csdn.net/download/weixin_38615720/10906803

昨天晚上,2018年9月11日,微信官方又更新了一大波的小程序功能。重点我们来谈谈这个功能,微信叫做统一服务消息下发接口。

  这个是官方的文档  统一服务消息 · 小程序
https://developers.weixin.qq.com/miniprogram/dev/api/notice-uniform.html

   统一服务消息下发接口,名字很拗口,说点人话,就是这个接口可以发送公众号或者小程序的消息。之前大家都知道,我们用小程序发送消息,有小程序的api接口,如果是发送公众号的消息,就有公众号的模板消息接口。

  现在终于微信出来这个统一发送的了。这个接口一出来我们就尝试使用了下。有些问题大家需要留意下,不然到时候会遇到不少的坑。

  access_token 小程序的access_token   这个一定要用小程序的accesstoken,不能使用公众号的,因为这个接口的初衷就是想大家在开发小程序的时候,如果要发送公众号消息直接使用这个接口就可以了,无需再去调用公众号的模板消息接口。

touser 用户openid,可以是小程序的openid,也可以是mp_template_msg.appid对应的公众号的openid     这个使用公众号的openid或者小程序的openid都可以的   微信官方会自动关联。其实大家的使用场景大部分还是在小程序,所以这里你就直接用小程序里抓到的openid就可以了。

mp_template_msg.appid 公众号appid,要求与小程序有绑定且同主体      这个大家要仔细读官方的说明哦,就是这公众号和小程序要相互关联了,且必须是同主体才行。不然也会调试不通过的。

  基本上只要掌握和留意了上面这几点,基本上就没问题了。如果还有问题,那就肯定是你传值不对了。你就要多检查了。最好自己打印出日志来。比如你传的值是什么,看看是否和文档里的演示demo一样。值的类型有没有传错之类的。

代码:

/**
     * 抽奖结果 公众号模板消息
     */
    public function integral_result(){
        $access_token= $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=".$access_token;
        $send_data = array();
        $send_data['touser'] = "发送人openid";
        $values = array();
        //公众号appid
        $values['appid'] = "";
        //公众号模板id
        $values['template_id'] = "";
        //公众号模板消息所要跳转的url
        $values['url'] = "http://weixin.qq.com/download";
        //公众号模板消息所要跳转的小程序,小程序的必须与公众号具有绑定关系
        $values['miniprogram'] = array(
            "appid"=>"",
            "path"=>"pages/index/index"   //微信公众号发送模板的坑
        );
        $values['data'] = array(
            "first"=>array(
                "value"=>"抽奖结果通知",
                "color"=>"#173177"
            ),
            "keyword1"=>array(
                "value"=>"最新的iphone一部,希望再接再励!!!",
                "color"=>"#173177"
            ),
            "keyword2"=>array(
                "value"=>"恭喜你中奖啦",
                "color"=>"#173177"
            ),
            "remark"=>array(
                "value"=>"点击查询详情",
                "color"=>"#173177"
            ),
        );
        $send_data['mp_template_msg'] = $values;
        $send_data_json = json_encode($send_data);
        $res = $this->http_curl($url,"post",$send_data_json);
        echo "<pre>";
        print_r($res);
        echo "</pre>";
        exit();

    }
    /**
      * 获取access_token
      */
    public function getWxAccessToken(){
          //缓存access_token
          if($_SESSION['access_token'] && $_SESSION['access_token_expire_time']>time()){
                $rs = $_SESSION['access_token'];
          }else{
                $appid = '小程序appid';
                $secret = '小程序secret';
                $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
                $res = $this->http_curl($url);
                $rs = $res['access_token'];
                $_SESSION['access_token'] = $rs;
                $_SESSION['access_token_expire_time'] = time()+7000;
          }
          return $rs;
    }
    /**
     * 封装curl
     */
    public function http_curl($url, $type = 'get', $arr ='',$res ='json'){
        $cl = curl_init();
            curl_setopt($cl, CURLOPT_URL, $url);
            curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1);
        if($type == 'post'){
            curl_setopt($cl, CURLOPT_POST, 1);
            curl_setopt($cl, CURLOPT_POSTFIELDS, $arr);
        }
        $output = curl_exec($cl);
        curl_close($cl);
        if($res == 'json'){
            if( curl_error($cl)){
                return curl_error($cl);
            }else{
                return json_decode($output, true);
            }
        }
    }

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值