Implement automatic sending of welcome messages to new users of WeChat public accounts in PHP

王林
Release: 2023-05-13 12:52:01
Original
1296 people have browsed it

In today’s social media era, WeChat public accounts have become an important means for many companies and individuals to establish online brands and social media marketing. For a new user, following an official account for the first time is an important node and the best time to establish a good relationship. At this time, it is very important to automatically send welcome messages. In PHP, it is not difficult to automatically send a welcome message through some simple code. Let's introduce the steps below.

The first step is to obtain the user’s attention event

In the WeChat public account, after the user follows the public account, the public account will receive an attention event. We need to write a code to obtain this Pay attention to the information. In PHP, user attention events can be obtained through the following code:

$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
    $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
    $fromUsername = $postObj->FromUserName;//用户openid
    $toUsername = $postObj->ToUserName;
    $event = $postObj->Event;//事件类型
    $time = time();
    if($event == "subscribe"){
        // 欢迎消息处理逻辑
    }
}
Copy after login

The second step is to write the welcome message processing logic

After obtaining the user attention events through the previous step, we need to write a welcome message processing logic. In PHP, we can construct a welcome message through the following code:

$welcomeMsg = "感谢关注我们的公众号,我们将会为您提供最全面的资讯和最贴心的服务。";
Copy after login

The third step is to send the welcome message

The main code for sending the welcome message is as follows:

$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $accessToken;
$data = '{
    "touser":"' . $fromUsername . '",
    "msgtype":"text",
    "text":
    {
         "content":"' . $welcomeMsg . '"
    }
}';
$result = postOpenSSL($url, $data);//post请求发送数据,调用定义好的函数postOpenSSL()
Copy after login

where , $accessToken is the access_token that needs to be obtained, which can be obtained through the following code:

$accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appId . "&secret=" . $appSecret;
$accessTokenData = httpsRequest($accessTokenUrl);
$accessToken = json_decode($accessTokenData, true)["access_token"];
Copy after login

Before sending the message, we need to define a postOpenSSL() function for sending HTTPS requests.

function postOpenSSL($url, $data){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    $headerArr = array('Content-Type: application/json', 'Content-Length: ' . strlen($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}
Copy after login

Here, we use curl to send an HTTPS request, set the request header and request data, and pay attention to turning off the verification of the SSL certificate.

Finally, through the above steps, we successfully implemented the function of automatically sending welcome messages to new users of the WeChat public account in PHP.

The above is the detailed content of Implement automatic sending of welcome messages to new users of WeChat public accounts in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template