With the rapid growth of mobile internet, WeChat Official Accounts have become an important platform for businesses and individuals to interact with users. To enhance user experience, custom reply functionality is essential. This article demonstrates how to implement custom replies using PHP with concrete code examples.
First, register for a WeChat Official Account developer account. After completing registration, you will receive an AppID and AppSecret for API access.
Create an official account and bind a domain, for example example.com. In the developer center, go to the “Server Configuration” section.
Fill in the server configuration:
After submitting, WeChat will send a GET request to verify the server, which should return the echostr to confirm success.
Create an api.php file on your server to handle incoming messages and events.
<?php // Verify server configuration $token = "my_token"; // Must match the token set in WeChat backend $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $echostr = $_GET["echostr"]; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode($tmpArr); $tmpStr = sha1($tmpStr); if ($tmpStr == $signature) { echo $echostr; exit; } // Handle received messages and events $postStr = file_get_contents("php://input"); if (!empty($postStr)) { $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $msgType = $postObj->MsgType; // Respond according to message type switch ($msgType) { case "text": $content = $postObj->Content; $responseText = "You sent: " . $content; replyText($postObj, $responseText); // Call function to reply text break; case "event": $event = $postObj->Event; if ($event == "subscribe") { $responseText = "Thank you for following our official account!"; replyText($postObj, $responseText); } break; // Handle other message types // ... } } // Function to reply with text messages function replyText($postObj, $content) { $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $time = time(); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $content); echo $resultStr; } ?>
The code above demonstrates server verification and handling of text messages and subscription events, replying according to the type of message received.
Upload the api.php file to your server and ensure the URL matches the server configuration.
You can send a text message or subscribe to the official account to verify that the custom reply works.
This article introduced the full process of implementing custom replies for WeChat Official Accounts using PHP, including developer registration, API permission setup, message and event handling, server deployment, and testing. Developers can modify and optimize the code according to actual requirements to create a fully functional official account service.