php 微信小程序 统一服务消息 订阅消息

先看腾讯文档:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/uniform-message/uniformMessage.send.html

通过文档可以看出,需要携带ACCESS_TOKEN,相关参考在https://blog.csdn.net/mushui0633/article/details/89469209

<?php

$appid='wx你的appid';
$appsecret='bd3你的小程序secret';
$r = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret");
$data = json_decode($r,true);

$url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$data['access_token'];
$data = array('touser' => 'ox客户的openid', //发给谁
    'template_id' => 'UU你的小程序统一服务消息ID——hBE',
    'page' => 'pages/my/li你的小程序前端页面地址和参数?id=ggf&shareId=ghf',
    'data' => array(
        'thing1' => array(
            'value' => '$goodTitle',
        ),
        'thing2' => array(
            'value' => '$orderPrice'.'元',
        ),
        'thing5' => array(
            'value' => '$orderTime',
        ),
        'time3' => array(
            'value' => '2019-12-11 18:36',
        ),
        // 这里如果日期时间格式不对就报错,比如:{"errcode":47003,"errmsg":"argument invalid! hint: [I1D5la06238630] data.time3.value invalid"}
    )
);
$data = json_encode($data);
$result = curl_post($url, $data);
$result = json_decode($result);

if ($result->errcode == '0' && $result->errmsg == 'ok') {
    echo $result;
    return 'success';
} else {

    return '发送失败';
}

function curl_post($url, $data)
{
    $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);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $response = curl_exec($ch);
    curl_close($ch);
    echo $response;
    return $response;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值