$appid = "********";
$appsecret = "************";
$TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;
$json = file_get_contents($TOKEN_URL);
$result = json_decode($json, true);
$ACCESS_TOKEN = $result['access_token'];
$template = array(
'touser' => '************',
'template_id' => "********",
'url' => "http://weixin.qq.com/download",
'topcolor' => "#FF0000",
'data' => array(
'first' => array('value' => urlencode('first'), 'color' => "#00008B"),
'店名' => array('value' => urlencode('店名'), 'color' => '#00008B'),
'销售额' => array('value' => urlencode('销售额'), 'color' => '#00008B'),
'客单数' => array('value' => urlencode('客单数'), 'color' => '#00008B'),
'remain' => array('value' => urlencode('remain'), 'color' => '#00008B'),
)
);
$json_template = json_encode($template);
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $ACCESS_TOKEN;
$res = $this->http_request($url, urldecode($json_template));
if ($res['errcode'] == 0) echo '消息发送成功!';
php 的话,我测试的方法是强制把用户的 openid 转为字符串类型。
参考一下https://developers.weixin.qq.com/community/develop/doc/0000089f4547f08d04daf3f4a5bc00?_at=1613989817778