以TP5为例,代码如下
/* * 模板消息 * $touser 接收者(用户)的 openid * $template_id 所需下发的模板消息的id * $page 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转 * $form_id 表单提交场景下,为 submit 事件带上的 formId;支付场景下,为本次支付的 prepay_id * $data 模板内容,不填则下发空模板。具体格式请参考示例 */ public function templateMessage($touser,$template_id,$page,$form_id,$data) { $datas['touser'] = $touser; $datas['template_id'] = $template_id; $datas['page'] = $page; $datas['form_id'] = $form_id; $datas['data'] = $data; $access_token = $this->getwxtoken(); $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$access_token; $res = https_request($url,$datas,'json'); return $res; } //获取token public function getwxtoken() { //$access_token = Cache::get('access_token'); //if($access_token){ // return $access_token; //}else{ $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".config('wechat.appid')."&secret=".config('wechat.secret'); $token_json = http_request($url); $tokenarr = json_decode($token_json,true); // Cache::set('access_token', $tokenarr['access_token'] ,3600); return $tokenarr['access_token']; //} }
调用
$openid = ''; $template_id = 'lNxLwcqCQpQR6xFhidoUTDiswWXEf15g-kstHBpNmp0'; $page = 'pages/index/index'; $form_id = $orderinfo['prepay_id']; $dataArr['keyword1'] = array('value'=>'商品购买成功'); $dataArr['keyword2'] = array('value'=>$orderinfo['ordernumber']); $dataArr['keyword3'] = array('value'=>'¥'.$orderinfo['price']); $dataArr['keyword4'] = array('value'=>date('Y-m-d H:i:s')); $this->templateMessage($openid,$template_id,$page,$form_id,$dataArr);
版权声明:本站所提供的文章、图片等内容均为用户发布或互联网整理而来,仅供学习参考,如有侵犯您的版权,请联系我们客服人员删除。