PHP 微信 一次性订阅消息 开发 (微擎、非微擎框架)

使用微擎开发一次性 消息 在web 端写的 功能首先 要绕过微擎的 登陆 直接访问 

链接 https://blog.csdn.net/fuchto/article/details/118190252

微信官方文档:https://developers.weixin.qq.com/doc/offiaccount/Message_Management/One-time_subscription_info.html

第一步 

发送链接 引导用户打开链接 https://mp.weixin.qq.com/mp/subscribemsg?action=get_confirm&appid=wxaba38c7f163da69b&scene=1000&template_id=1uDxHNXwYQfBmXOfPJcjAS3FynHArD8aWMEFNRGSbCc&redirect_url=http%3a%2f%2fsupport.qq.com&reserved=test#wechat_redirect

appid 为 当前公众号 appid 

template_id 在公众号后台接口权限中查看

redirect_url 为点击之后跳转的位置  (地址必须使用urlencode编码)前提条件 当前域名必须在微信公众号后台中 已设置为业务域名

在点击之后跳转页面 微信返回数据

参数说明
openid用户唯一标识,只在用户确认授权时才会带上
template_id订阅消息模板ID
action用户点击动作,"confirm"代表用户确认授权,"cancel"代表用户取消授权
scene订阅场景值
reserved请求带入原样返回

通过template_id 获取对应公众号的相关配置

跳转页面后 得判断 action 是同意 还是 取消 如果不判断都会跳转到 同意的页面执行对应的逻辑

 if ( isset($_GET['action']) && $_GET['action'] == 'confirm') {
                template('platform/home');
    } elseif ( isset($_GET['action']) && $_GET['action'] == 'cancel') {
        echo "请同意授权";
    }else {
        echo("请返回上一步操作");
    }

跳转到对应的页面之后 通过ajax 发送模版消息

$('button').on('click',function () {
		$.ajax({
			type : 'post',
			url : "url 地址",//(微擎使用 index.php/?c=xxx&a=xxx&do=xxx)
			data : {
				scene : "{$_GET['scene']}",
				openid : "{$_GET['openid']}",
				template_id : "{$_GET['template_id']}",
			},
			dataType : 'json',
			success:function(datas){
				console.log(datas);
				if(datas.errcode == 0){
                    // 关闭微信当前页面
					WeixinJSBridge.call('closeWindow');
				}else{
					alert('消息发送失败');
				}

			}
		})
	})

发送 一次性订阅消息 (微擎) (传入的数据必须为 json 数据类型 否则报错 , 如果设置了模版内容的颜色 必须传入 16进制的格式 否则 在苹果中 模版消息内容为空 不显示)

 $info = pdo_get('subscribemsg',['template_id' => $_POST['template_id']]);
    if(!$info) die("模版内容不存在");
    $account = pdo_get('account_wechats',['uniacid'=>$info['uniacid']]);
    if (!$account) die("公众号不存在");
// 由于当前 绕过了 微擎框架的 登陆校验 微擎框架 全局变量$_W['uniacid'] 不存在 需要手动 传入 有时候报错 是因为方法里有些重复调用的方法没有获取到 uniacid 报错需要手动添加
    $account_api = WeAccount::create($info['uniacid']);
    $token = $account_api->getAccessToken(); // 获取对应 公众号的token
    $url = "https://api.weixin.qq.com/cgi-bin/message/template/subscribe?access_token={$token}";

    $data = [
        // 用户openid
        'touser' => $_POST['openid'],
        // 模版id
        'template_id' => $_POST['template_id'],
        // 点击 模版消息 跳转链接
        'url' => $info['jump_url'],
        // 场景值
        'scene' => $_POST['scene'],
        // title
        'title' => $info['msg_title'],
        'data' => [
//            字体颜色为 十六进制格式  否则为空
            'content' => ['value' => $info['msg_text'], 'color' => empty($info['msg_color'])?"#000000":$info['msg_color']], 
        ]
    ];
// 发送 消息
    $response = ihttp_post($url,json_encode($data));
    // 微擎直接 输出即可
    echo ($response['content']);

非微擎框架 可以使用 easwechat 发送请求 更为简洁  (微擎不能使用easwechat 因为如果使用的话 微擎框架中已经 有过 微信 token 的存储 ,如果使用easwechat 的话 会不断刷新 token)

 $info = db('content')->where(['template_id' => $_GET['template_id']])->find();
// 设置配置
        $config = [ 
// appid
            'app_id' => $info['appid'],
// app秘钥
            'secret' => $info['app_secret'],
        ];

        $app = Factory::officialAccount($config);
// 发送 模版消息
        $result = $app->template_message->sendSubscription([
            // 用户openid
            'touser' => $_GET['openid'],
            // 模版id
            'template_id' => $_GET['template_id'],
            // 点击跳转链接
            'url' => $info['jump_url'],
            // 场景值
            'scene' => $_GET['scene'],
            // title
            'title' => $info['msg_title'],
            'data' => [
// 颜色必须使用 16进制 
                    'content' => ['value' => $info['msg_text'], 'color' => empty($info['msg_color'])?"black":$info['msg_color']], 
            ]
        ]);
        return json(['data'=>$result]);

easwechat 文档地址 https://easywechat.com/docs/5.x/official-account/template_message

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fuchto

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值