WeChat Message Template-Jukeng

The WeChat platform is divided into active messages and passive messages. The active message is the production server actively pushing messages to the WeChat platform, and the passive message is the user behavior that causes the WeChat platform to push messages to the production server.


  • Proactive messages need:
    • 1. Add the production server ip to the ip whitelist
    • 2. Then get token according to appid and secret
    • 3. Put the token& on the url every time you actively push

  • Passive message:
    • 1. Fill in the url path of the production server on the official account configuration
    • 2. Get the token binding

1. Message template

  • 1. Get the WeChat message template api interface link (Jukeng, there is a problem with the message template api link on the WeChat official account document, the correct one should be found on the WeChat official account, message template settings, and interface document)

  • 2. Get the message template id

  • 3. Get the openid of the user who needs to send a message (follow the official account)

  • 4. Construct the message template data

  • 5. Success sign:

    • (1) Receive the returned json data, error=0,
    • (2) WeChat platform actively pushes xml to the server

2. Matters needing attention

  • 1. WeChat template api is the official account, message template plug-in, in the message template interface document

  • 2. Message template syntax:

    • (1) Data format structure, json format
    • (2) Send by post
  • 3. The user's openid must, must, must be under the official account appid, that is to say, it must be the user who has followed the official account.

  • 4. Jukeng, Jukeng, and the openid of the message template is not the only openid of WeChat, but the tofakeid of each official account corresponding to the user

    • The so-called "openid" of the WeChat public platform is divided into two types:

      • [The first type of openid] is obtained from the interactive xml:
        <![CDATA[fromUser]]>
        Each user is unique for each WeChat public platform, that is, the same user has different WeChat public platforms. [The first type of openid] is Different; I
        tend to call it fakeid, and get the page without authorization.
      • [The second type of openid] is to use the OAuth2.0 interface provided by WeChat (the advanced interface needs to be an authenticated service number)
        , which is unique for each user, which means that the same user’s [second type of openid] is all on different WeChat public platforms The same; I
        personally tend to call it a real openid, and get a page that requires authorization.
        The first type of public platform developer documentation is not stated clearly, but it is not difficult to infer as long as you have read the documentation; the
        second type of interface used is detailed in: Public platform developer documentation-webpage authorization to obtain basic user information.

ps: detailed script code:

 /**
     * @param $open_id	用户openid
     * @param $template_id	消息模板id
     * @param $miniprogram	消息模板跳转的appid,pagepath页面链接,可根据自我需求删改
     * @param $appid	公众号appid
     * @param $num		申请编号,可根据自我需求删改
     * 发送消息模板
     * @return array
     */
    public  function SendMsg($open_id,$template_id,$miniprogram,$num){
        try {
            $token = $this->get_token();
        } catch (\Exception $e) {
       		 $e->getMessage();
        }
        //发送模板消息url
        $send_url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$token}";
        $date=date("Y-m-d H:i:s",time());
        $data=[
                "touser"=>$open_id,
                "template_id"=>$template_id,
                'miniprogram'=>$miniprogram,
                "data"=>[
                    "first"=>[
                        "value"=>"吸易商城提醒您,您的消杀片已到期,请及时更换",
                    ],
                    "keyword1"=>[
                        "value"=>"商品更换提醒",
                    ],
                    "keyword2"=>[
                        "value"=>$num,
                    ],
                    "keyword3"=>[
                        "value"=>$date,
                    ],
                    "keyword4"=>[
                        "value"=>'待处理',
                    ],
                    "remark"=>[
                        "value"=>"详情信息,请戳详情了解",
                    ],
                ],
        ];
    
        //$postData = http_build_query($postData); //做一层过滤
        $json_data=json_encode($data);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,            $send_url);
        curl_setopt($ch, CURLOPT_HEADER, 0); //设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($ch, CURLOPT_POST,           1 );
        curl_setopt($ch, CURLOPT_POSTFIELDS,     $json_data );
        $request=curl_exec ($ch);
        curl_close($ch);
        $request=json_decode($request,true);
        if ($request['errcode']==0){
            return [
                'code'=>ApiCode::CODE_SUCCESS,
                'msg'=>"消息模板发送成功",
            ];
        }
        return [
            'code'=>ApiCode::CODE_ERROR,
            'msg'=>$request,
        ];
    }

Guess you like

Origin blog.csdn.net/weixin_43272542/article/details/113568308