首先我們需要申請(qǐng)一個(gè)公眾號(hào)
登錄到后臺(tái)以后按照下列操作執(zhí)行
1.添加功能插件2.找到模板消息
3.好了殴蓬,接下來你可以申請(qǐng)了,隨便選兩個(gè)行業(yè)就可以了蟋滴,審核通過以后進(jìn)去就可以添加模板了
4.添加好模板以后看格式
箭頭指向的地方是我們需要用的,方框的東西就是參數(shù)!
詳細(xì)看代碼(這里使用yii做的)
<?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(){
//獲取參數(shù)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');
//形成數(shù)組然后按照字典排序
$array = array();
$array = array($nonce,$timestamp,$token);
sort($array);
//拼接字符串痘绎、sha1加密津函,然后與signature進(jìn)行效驗(yàn)
$str = sha1(implode($array));
//這里是第一次調(diào)用,主要用來在基本設(shè)置中開啟啟用配置
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'){
//如果是關(guān)注事件
if(strtolower($postObj->Event) == 'subscribe'){
//多圖文
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$time = time();
$Msgtype = 'news';
$arr = array(
array(
'Title'=>'歡迎關(guān)注AD共享',
'Description'=>'這是一個(gè)屬于互聯(lián)網(wǎng)傳媒的時(shí)代',
'PicUrl'=>'http://peita.oss-cn-beijing.aliyuncs.com/question/546781498411128.jpg',
'Url'=>'http://www.peita.net'
)
);
$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;
/*文本
//回復(fù)用戶消息
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$time = time();
$Msgtype = 'text';
$Content = '歡迎關(guān)注AD共享公眾號(hào)';
$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;*/
}
}
}
//發(fā)送模版消息
public function actionPushMessage(){
//獲取token
$token = self::actionGetToken();
//設(shè)置url
$url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$token;
//設(shè)置時(shí)間
$sj = date('Y-m-d H:i:s',time());
//設(shè)置充值鏈接
$cz_url = 'http://www.peita.net';
//設(shè)置賬戶余額
$ye = '10元';
//設(shè)置賬戶名稱
$name = '小郭';
//設(shè)置發(fā)送的消息
$message = [
'touser'=>'這里是用戶的openid',
'template_id'=>'SudqvwsNYy4AFqBO4_KaWklYlhrc69c74jiAF3VBoCU',
'url'=>$cz_url,
'data'=>[
'first'=>['value'=>'您好,您的賬戶余額不足孤页。請(qǐng)盡快充值尔苦,以免影響您的使用。','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'=>'如有任何疑問請(qǐng)登錄會(huì)員系統(tǒng)聯(lián)系在線客服','color'=>'#173177']
]
];
$data = json_encode($message);
//發(fā)送
$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請(qǐng)求行施,支持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;
}
}
好了允坚,詳細(xì)效果就可以出來了
要注意的是提前配置好線上的域名服務(wù)器等等,并且在微信的基本配置中啟用一下基本配置蛾号!
效果圖: