PHP implements WeChat template message to send to specified user

I have received a lot of inquiries from children's shoes. Can I send a message to a certain user using the WeChat public account interface? If it is sent directly, there are some restrictions. One is the customer service message interface, but this requires the user to communicate with the public within 48 hours. The number can only be sent if there is interaction. The other is the template message of WeChat, which has no time limit for sending, but WeChat needs to use a template before sending it in order to prevent some junk data. I believe that we usually receive a lot of template messages, and the actual effect is as follows:

How to achieve the effect of this WeChat template message?

First, you need to select the template message in the "Function" menu in the background of the WeChat public platform. If there is no template message in the menu, you need to click the function plug-in to add it, as shown below:

 

After adding a plug-in, you can select a template to use in the template library. Of course, you can also create a template. Generally speaking, the template library is enough. After selecting a template, you must remember the template ID, which will be used in subsequent PHP programs.

Next, let's talk about the calling rules of the interface:

http request method: POST
https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN

What format of data to send? The data sent is in json format, take the following template as an example:

The JSON format is as follows:

{
    "touser": "Here is the receiving user OPENID",
    "template_id": "here is the template ID",
    "url": "Here is the URL that jumps after clicking on the details",
    "data": {
        "first": {
            "value": "Hello!A visitor has left a message for you.",
            "color": "#173177"
        },
        "user": {
            "value": "Zhang San",
            "color": "#FF0000"
        },
        "ask": {
            "value": "Hello,Very concerned about Dawn Interconnect,Is there any video tutorial about Alipay?",
            "color": "#173177"
        },
        "remark": {
            "value": "This user has been registered for 12 days",
            "color": "blue"
        }
    }
}

Now that we understand the data format, how to send it to the server using PHP? In addition, can JSON only be defined as a string completed by data splicing in the code? If you are not familiar with strings or do not like strings, you can use arrays to convert them. Take an array as an example to give a PHP code snippet to implement a WeChat template message:

class WxTmp
{
    //The address of the request template message
    const TEMP_URL = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=';
    public function getAccessToken(){
        //Get it here accesstoken  Please modify according to your own program
        return $access_token;
    }
    /**
    * WeChat template message sending
    * @param $openid Receive user's openid
    * return send result
    */
    public function send($openid){
        $tokens = $this->getAccessToken();
        $url = self::TEMP_URL . $tokens;
        $params = [
            'touser' => $openid,
            'template_id' => 'Oblr5uXH_fS79gMC8E0mYz0CpUAHnJtdvAC3PWABrsk',//template ID
            'url' => 'https://www.liminghulian.com/course/3/lesson/list', //After clicking the details URL can be dynamically defined
            'data' => 
                    [
                      'first' => 
                         [
                            'value' => 'Hello!A visitor has left a message for you.',
                            'color' => '#173177'
                         ],
                      'user' => 
                         [
                            'value' => 'Zhang San',
                            'color' => '#FF0000'
                         ],

                      'ask' => 
                         [
                                'value' => 'Hello,Very concerned about Dawn Interconnect,Is there any video tutorial about Alipay?',
                                'color' => '#173177'
                         ],
                       'remark' => 
                         [
                                'value' => 'This user has been registered for 12 days',
                                'color' => 'blue'
                         ] 
                      ]
        ]; 
        $json = json_encode($params,JSON_UNESCAPED_UNICODE);
        return $this->curlPost($url, $json);
    }
    /**
    * Send data via CURL
    * @param $url the requested URL
    * @param $data data sent
    * return request result
    */
    protected function curlPost($url,$data)
    {
        $ch = curl_init();
        $params[CURLOPT_URL] = $url;    //ask url address
        $params[CURLOPT_HEADER] = FALSE; //Whether to return response header information
        $params[CURLOPT_SSL_VERIFYPEER] = false;
        $params[CURLOPT_SSL_VERIFYHOST] = false;
        $params[CURLOPT_RETURNTRANSFER] = true; //whether to return the result
        $params[CURLOPT_POST] = true;
        $params[CURLOPT_POSTFIELDS] = $data;
        curl_setopt_array($ch, $params); //incoming curl parameter
        $content = curl_exec($ch); //implement
        curl_close($ch); //close the connection
        return $content;
    }
}

$obj = new WxTmp();
$openid = 'of the user who received the template information openid';
echo $obj->send($openid);

 

Posted by ganlal on Wed, 01 Jun 2022 02:27:33 +0530