申请一个公众号

首先我们需要申请一个公众号

登录到后台以后按照下列操作执行

1.添加功能插件申请一个公众号_第1张图片

 

 

2.找到模板消息

申请一个公众号_第2张图片

 

 

3.好了,接下来你可以申请了,随便选两个行业就可以了,审核通过以后进去就可以添加模板了

 

4.添加好模板以后看格式

 

申请一个公众号_第3张图片

 

 箭头指向的地方是我们需要用的,方框的东西就是参数!

 

详细看代码(这里使用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(){
        //获取参数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'){

      

//多图文 

$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'
)
);

$template = "


%s

".count($arr)."
";

foreach($arr as $key=>$v){
$template .="
<![CDATA[".$v['Title']."]]> 



";
}

$template .="

";
$info = sprintf($template,$toUser,$fromUser,$time,$Msgtype);
echo $info;


/*文本
//回复用户消息 $toUser = $postObj->FromUserName; $fromUser = $postObj->ToUserName; $time = time(); $Msgtype = 'text'; $Content = '欢迎关注AD共享公众号'; $template = " %s "; $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; } }
复制代码

好了,详细效果就可以出来了

 

要注意的是提前配置好线上的域名服务器等等,并且在微信的基本配置中启用一下基本配置!

效果图:

申请一个公众号_第4张图片

 

你可能感兴趣的:(申请一个公众号)