首先咱們須要申請一個公衆號php
登陸到後臺之後按照下列操做執行html
1.添加功能插件json
2.找到模板消息api
3.好了,接下來你能夠申請了,隨便選兩個行業就能夠了,審覈經過之後進去就能夠添加模板了數組
4.添加好模板之後看格式服務器
箭頭指向的地方是咱們須要用的,方框的東西就是參數!微信
詳細看代碼(這裏使用yii作的)app
<?php namespace backend\modules\api\controllers; use common\models\SmsReport; use common\models\Aes; use common\models\Counter; use common\models\Phone; use common\models\City; use common\models\oss\oss; use Yii; use yii\rest\Controller; use backend\modules\api\common\lib\Helper; use backend\modules\api\models\User; use backend\modules\api\models\Register; use backend\modules\api\common\lib\HttpUploadFile; use yii\base\Object; use backend\modules\api\models\Login; use backend\modules\api\models\Abnormal; header('Access-Control-Allow-origin:*'); header('Access-Control-Allow-Credentials:true'); header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS'); header('Access-Control-Allow-Headers:Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With'); header('Content-Type: text/html; charset=utf-8'); class WechatController extends Controller { public function actionIndex(){ //獲取參數signature nonce token timestamp echostr $nonce = Yii::$app->request->get('nonce'); $token = 'adshare'; $timestamp = Yii::$app->request->get('timestamp'); $echostr = Yii::$app->request->get('echostr'); $signature = Yii::$app->request->get('signature'); //造成數組而後按照字典排序 $array = array(); $array = array($nonce,$timestamp,$token); sort($array); //拼接字符串、sha1加密,而後與signature進行效驗 $str = sha1(implode($array)); //這裏是第一次調用,主要用來在基本設置中開啓啓用配置 if($str == $signature && $echostr){ return $echostr; }else{ self::actionMessage(); } } public function actionMessage(){ //獲取微信推送的xml $postArr = $GLOBALS['HTTP_RAW_POST_DATA']; $postObj = simplexml_load_string($postArr); if(strtolower($postObj->MsgType) == 'event'){ //若是是關注事件 if(strtolower($postObj->Event) == 'subscribe'){
//多圖文 yii
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$time = time();
$Msgtype = 'news';
$arr = array(
array(
'Title'=>'歡迎關注AD共享',
'Description'=>'這是一個屬於互聯網傳媒的時代',
'PicUrl'=>'http://peita.oss-cn-beijing.aliyuncs.com/question/546781498411128.jpg',
'Url'=>'http://www.peita.net'
)
);curl
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>".count($arr)."</ArticleCount>
<Articles>";
foreach($arr as $key=>$v){
$template .="<item>
<Title><![CDATA[".$v['Title']."]]></Title>
<Description><![CDATA[".$v['Description']."]]></Description>
<PicUrl><![CDATA[".$v['PicUrl']."]]></PicUrl>
<Url><![CDATA[".$v['Url']."]]></Url>
</item>";
}
$template .="</Articles>
</xml>";
$info = sprintf($template,$toUser,$fromUser,$time,$Msgtype);
echo $info;
/*文本 //回覆用戶消息 $toUser = $postObj->FromUserName; $fromUser = $postObj->ToUserName; $time = time(); $Msgtype = 'text'; $Content = '歡迎關注AD共享公衆號'; $template = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $info = sprintf($template,$toUser,$fromUser,$time,$Msgtype,$Content); echo $info;*/ } } } //發送模版消息 public function actionPushMessage(){ //獲取token $token = self::actionGetToken(); //設置url $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$token; //設置時間 $sj = date('Y-m-d H:i:s',time()); //設置充值連接 $cz_url = 'http://www.peita.net'; //設置帳戶餘額 $ye = '10元'; //設置帳戶名稱 $name = '小郭'; //設置發送的消息 $message = [ 'touser'=>'這裏是用戶的openid', 'template_id'=>'SudqvwsNYy4AFqBO4_KaWklYlhrc69c74jiAF3VBoCU', 'url'=>$cz_url, 'data'=>[ 'first'=>['value'=>'您好,您的帳戶餘額不足。請儘快充值,以避免影響您的使用。','color'=>'#173177'], 'keyword1'=>['value'=>$name,'color'=>'#173177'], 'keyword2'=>['value'=>$ye,'color'=>'#173177'], 'keyword3'=>['value'=>$cz_url,'color'=>'#173177'], 'keyword4'=>['value'=>$sj,'color'=>'#173177'], 'remark'=>['value'=>'若有任何疑問請登陸會員系統聯繫在線客服','color'=>'#173177'] ] ]; $data = json_encode($message); //發送 $res = self::actionCurlRequest($url,$data); return $res; } //獲取token public function actionGetToken(){ $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET'; $res = json_decode(self::actionCurlRequest($url)); return $res->access_token; } //curl請求,支持post和get public function actionCurlRequest($url,$data=null){ $curl = curl_init(); curl_setopt($curl,CURLOPT_URL,$url); 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); $output = curl_exec($curl); curl_close($curl); return $output; } }
好了,詳細效果就能夠出來了
要注意的是提早配置好線上的域名服務器等等,而且在微信的基本配置中啓用一下基本配置!
效果圖:
參考網址:http://www.cnblogs.com/txw1958/p/wechat-template-message.html
參考網址:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277