2021-07-22


发送小程序和公众号统一的服务消息 AND 公众号模板消息


小程序和公众号统一的服务消息 是可以配置小程序和公众号同时发送模板消息的(小程序模板消息已废弃),在小程序最适合使用此方法发送公众号模板消息**

1.appid screct 都是小程序端的,此方法本来就是为了小程序而发布
2.mp_template_msg 为公众号模板消息配置 因此APPID应为 公众号APPID
3.mp_template_msg > miniprogram 模板消息跳转路径配置 APPID为小程序APPID
4.本应还有小程序的模板消息配置,但已废弃,故不做解释
5.一定要记得转化json格式

	小程序和公众号统一的服务消息
		
```<?php
	public function Applet(){
	//小程序APPID
		$openid = "openid";
		//小程序screct
        $appid = "appid";
        $appScrect="screct";
       	//首先获取access_token 接口凭证
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appScrect}";
         $token =  json_decode(file_get_contents($url),true)['access_token'];

$datas=[
 //与公众号模板消息一一对应否则会出错
    "touser"=>$openid, //接收用户的openid
    "mp_template_msg"=>[
   	 //公众号APPID
        "appid"=>"APPID",
        "template_id"=>"模板ID",
        "miniprogram"=>[
        //小程序APPID
            "appid"=>"APPID",
            "pagepath"=>"pages/index/index"
        ],
        "data"=>[
            "first"=>[
                "value"=> '您的设备已离线', //自定义参数
                "color"=> '#173177'//自定义文字颜色
            ],
            "keyword1"=>[
                "value"=> '47006253565', //自定义参数
                "color"=> '#173177'//自定义文字颜色
            ],
            "keyword2"=>[
                "value"=> '2017-11-06 12:24:42', //自定义参数
                "color"=> '#173177'//自定义文字颜色
            ],
            "remark"=>[
                "value"=> '自己看吧', //自定义参数
                "color"=> '#173177'//自定义文字颜色
            ]
        ]

    ]
];
 $send = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=".$token;      
 $this->curl_post($send,json_encode($data));
}
 //公众号模板消息发送  配置模板消息项略有不同,APPID及SCRECT 都是公众号的
 public function official (){
		$openid = "openid";
		//公众号APPID
        $appid = "appid";
        //公众号screct
        $appScrect="screct";
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$app}&secret={$appScrect}";
         $token =  json_decode(file_get_contents($url),true)['access_token'];
$template=[
	    	'touser'      => '填写用户openid',  //用户openid
	      	'template_id' => "模板id", //在公众号下配置的模板id
	      	'miniprogram' => [
                'appid'    => '这里填写要跳转的小程序appid',
                 'pagepath' => 'pages/index/index?order_id=205', //这里填写小程序路径,可以拼接参数
             ],
	      	'topcolor'    => "#7B68EE",
	      	 "data"=>[
                "first"=>[
                    "value"=> '您的设备已离线', //自定义参数
                    "color"=> '#173177'//自定义文字颜色
                ],
                "keyword1"=>[
                    "value"=> '47006253565', //自定义参数
                    "color"=> '#173177'//自定义文字颜色
                ],
                "keyword2"=>[
                    "value"=> '2017-11-06 12:24:42', //自定义参数
                    "color"=> '#173177'//自定义文字颜色
                ],
                "remark"=>[
                    "value"=> '自己看吧', //自定义参数
                    "color"=> '#173177'//自定义文字颜色
                ]
        	]
    ]
    
 $send = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=".$token;      
 $this->curl_post($send,json_encode($data));
}
 public function curl_post($url , $params=array()){
	    $ch = curl_init();
	    curl_setopt($ch, CURLOPT_URL, $url);
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	    // POST数据
	    curl_setopt($ch, CURLOPT_POST, 1);
	    // 把post的变量加上
	   // $data=http_build_query($params);
	    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
	    $output = curl_exec($ch);
	    curl_close($ch);
	    return $output;
    }
?>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值