用户下完订单以后,自动推送订单消息到用户微信账号里。PHP根据openid向微信用户推送模板消息
首先到mp.weixin.qq.com,配置微信模板消息,获取appid和secret
然后获取微信用户的openid,之前我们在这篇文章中演示过http://www.sucaihuo.com/php/1008.html
include_once 'config.php';
$redirect_uri = "http://www.sucaihuo.com/project/openid/openid.php";
$redirect_uri = urlencode($redirect_uri);
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $appid . "&redirect_uri=" . $redirect_uri . "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
header('Location: ' . $url . '');
然后获取access_token
$json_token = http_request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret);
$access_tokens = json_decode($json_token, true);
$access_token = $access_tokens['access_token'];
最后推送模板消息,模板id 必须是你自己的
//模板消息
$template = array(
'touser' => $openid, //用户openid
'template_id' => "Wi-ijSlQPzlnNjEkumVmfPKk9OUIfGcKyOn4Tky4iYM", //模板消息id
'url' => "http://www.sucaihuo.com/",
'topcolor' => "#173177",
'data' => array(
'first' => array('value' => urlencode("您好,您已购买成功"), 'color' => "#743A3A"),
'delivername' => array('value' => urlencode("顺丰快递"), 'color' => '#173177'),
'ordername' => array('value' => urlencode(date('YmdHis')), 'color' => '#173177'),
'remark' => array('value' => urlencode('请尽快发货'), 'color' => '#173177'),
)
);
$json_template = json_encode($template);
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
$json = http_request($url, urldecode($json_template));
$rs = json_decode($json, true);
if ($rs['errcode'] == 0) {
echo "<span style='color:red;font-size:32px'>模板消息发送成功,请打开微信查看是否有新消息。</span>";
} else {
echo "发送失败,错误信息如下:";
print_r($rs);
}
系统已开启自动识别垃圾评论机制,识别到的自动封号,下载出错或者资源有问题请联系全栈客服QQ 1915635791