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

【微信小程序】發送消息模板教程(后臺以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、歡迎各位大佬進行批評和指正,歡迎討論。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容