微信小程序开发开源工具技巧PHP经验分享

【微信小程序】发送消息模板教程

2017-07-07  本文已影响435人  第九程序官方

【微信小程序】发送消息模板教程(后台以PHP示例):

1、本教程对外开放,未经博主同意,禁止转载。

2、准备材料:1)公众号|小程序,添加选择的模板消息,2)在设置>开发设置页面,开通消息模板功能;如:

3、因为调用微信发送模板的接口是:https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=$access_token,本来直接在小程序端发送就好啦,结果api.weixin.qq.com域名不能添加到公众号request合法域名,所以只能在后台发起请求

4、本教程以报名成功消息模板为示例,微信示例如图:

5、实际效果图:

6、js端需要获取的数据:

1)其中,access_token是微信推送的token,你需要把它放在服务器缓存,有效期7200s;过期就请求、替换。

2)touser:目标发送人,也就是你要发送的openid,放在小程序端就行了。(有个bug,微信用户在同一手机切换,openid会造成混乱,我们暂时不考虑这个)

3)template_id:模板id,不多解释

4)page:上面图片5,进入小程序查看==>进的就是这个页面(可以不填)

5)form_id:获取的wxml表单form的id

6)keyword系列:发送的自定义主题数据,需要与模板数据(格式可以不一致)一致

7)url:L,对应后台的php地址,那里才是调用api的真正地址。这里就是把前台的数据给传到后台,让服务器发起调api.weixin.qq.com接口

8)

color: '#ccc',   =》解释下:发送的所有字体颜色,默认黑色

emphasis_keyword: 'keyword1.DATA'   =》解释下:需要放大的keyword1,默认没有

7、wxml页面:记得一定要加上【report-submit】,不然就没有form_id参数哦。

            姓  名:text>            view>    确认参加button>form>

8、后台服务器PHP页面:


/* https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' + that.data.access_token;      * 微信规定:不能直接在小程序调用,只能在后台发起    *  -xzz0704      */    public function send_msg(){              $data = $_POST;$access_token = I('POST.access_token');

$touser = I('POST.touser');

$template_id = I('POST.template_id');

$page = I('POST.page');

$form_id = I('POST.form_id');$keyword1 = I('POST.keyword1');               

$fee = I('POST.keyword4')?I('POST.keyword4'):'免费';  //活动费用,默认0.00免费                                /*                  * 根据活动id,获取活动对应的地址信息,$keyword3  -xzz 0705                */                

$a_id = I('POST.keyword3');                

$msg = M('activity','xxf_witkey_')->where('a_id='.

$a_id)->field('a_id,act_name,province,city,town,address')->find();                

$province = M('district','xxf_witkey_')->where('id='.$msg['province'])->getField('name');                

$city = M('district','xxf_witkey_')->where('id='.$msg['city'])->getField('name');                

$town = M('district','xxf_witkey_')->where('id='.$msg['town'])->getField('name');                

$keyword3 = $province.$city.$town.$msg['address'];                                if(empty($keyword1)){                    exit('empty activity message!');                }

$value= array(                    "keyword1"=>array(                    "value"=>I('POST.keyword1'),                    //"value"=>'woshihaoren',                    "color"=>"#4a4a4a"                    ),                    "keyword2"=>array(                        "value"=>I('POST.keyword2'),                        "color"=>"#9b9b9b"                    ),                    "keyword3"=>array(                        "value"=>$keyword3,                        "color"=>"#9b9b9b"                    ),                    "keyword4"=>array(                        "value"=>$fee,                        "color"=>"#9b9b9b"                    ),                    "keyword5"=>array(                        "value"=>I('POST.keyword5'),                        "color"=>"#9b9b9b"                    ),                    "keyword6"=>array(                        "value"=>I('POST.keyword6'),                        "color"=>"#9b9b9b"                    ),                    "keyword7"=>array(                        "value"=>I('POST.keyword7'),                        "color"=>"#9b9b9b"                    ),                    "keyword8"=>array(                        "value"=>I('POST.keyword8'),                        "color"=>"#9b9b9b"                    ),                    "keyword9"=>array(                        "value"=>I('POST.keyword9'),                        "color"=>"red"                    )                );

$url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$access_token;$dd = array();                

//$dd['access_token']=$access_token;                

$dd['touser']=$touser;                

$dd['template_id']=$template_id;                

$dd['page']=$page;  //点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,该字段不填则模板无跳转。      

$dd['form_id']=$form_id;$dd['data']=$value;//模板内容,不填则下发空模板                                

$dd['color']='';                        

//模板内容字体的颜色,不填默认黑色                

//$dd['color']='#ccc';                

$dd['emphasis_keyword']='';    //模板需要放大的关键词,不填则默认无放大                //$dd['emphasis_keyword']='keyword1.DATA';                                

//$send = json_encode($dd);  //二维数组转换成json对象                               

 /* curl_post()进行POST方式调用api: api.weixin.qq.com*/$result = $this->https_curl_json($url,$dd,'json');if($result){                    echo json_encode(array('state'=>5,'msg'=>$result));                }else{                    echo json_encode(array('state'=>5,'msg'=>$result));                }    }    /* 发送json格式的数据,到api接口 -xzz0704  */    function https_curl_json($url,$data,$type){        if($type=='json'){//json $_POST=json_decode(file_get_contents('php://input'), TRUE);            

$headers = array("Content-type: application/json;charset=UTF-8","Accept: application/json","Cache-Control: no-cache", "Pragma: no-cache");            $data=json_encode($data);        }        

$curl = curl_init();        

curl_setopt($curl, CURLOPT_URL, $url);        

curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求        

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);        

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);        if (!empty($data))

{            

curl_setopt($curl, CURLOPT_POST, 1);            

curl_setopt($curl, CURLOPT_POSTFIELDS,$data);       

 }        

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);        

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers );        

$output = curl_exec($curl);        if (curl_errno($curl)) 

{            

echo 'Errno'.curl_error($curl);//捕抓异常        

}        

curl_close($curl);        return $output;   

 }

9、本教程到此结束,最后献出服务器缓存微信access_token的代码:

/* 调用微信api,获取access_token,有效期7200s -xzz0704 */    public function get_accessToken()

{

        /* 在有效期,直接返回access_token */        

if(S('access_token')){            

echo S('access_token');        

}        

/* 不在有效期,重新发送请求,获取access_token */        

else{            

$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=YourAppid&secret=YourXiaochengxuSecret';           

 $result =curl_get_https($url);

//就是一个普通的get方式调用https接口的请求,我就不写出来了,自己找去。           

 $res = json_decode($result,true);  //json字符串转数组                       

 if($res)

{                

S('access_token',$res['access_token'],7100);                

echo S('access_token');            

}

else

{                

echo 'api return error';            

}        

}    

}

10、欢迎各位大佬进行批评和指正,欢迎讨论。

上一篇下一篇

猜你喜欢

热点阅读