With the advent of the Internet era, various emerging social media have emerged one after another. As an emerging social media, WeChat official account has become one of the must-haves for many people’s daily social interactions. As a developer, if you want to build a WeChat public account yourself, you must master certain skills. This article will introduce how to build a WeChat public account through PHP code.
1. Register a WeChat public platform account
If you want to set up a WeChat public account, you first need to register an account on the WeChat public platform. The registration process is not complicated. You only need to fill in relevant information, such as official account name, type, certification type, etc. Note that if your official account requires certification, you also need to submit relevant materials for certification.
2. Obtain appID and appsecret
After registering the WeChat official account, we need to obtain appID and appsecret. These two parameters are important parameters for connecting your own server and the WeChat public platform. Log in to the WeChat public platform, find the "Developer ID" in the Developer Center, and click to enter to obtain the appID and appsecret.
3. Build a server environment
After obtaining the appID and appsecret, we need to build a server to connect our application and the WeChat public platform. If you don't have your own server, you can choose to deploy your application on a cloud server, such as Alibaba Cloud, Tencent Cloud, etc.
4. Write PHP code
After the server environment is set up, we need to write PHP code to communicate with the WeChat public platform. The specific steps are as follows:
The specific code is as follows:
<?php //获取微信公众平台的请求参数 function getToken() { $token = "yourtoken"; //此处输入你的token值 $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $signature = $_GET["signature"]; $echostr = $_GET["echostr"]; $arr = array($timestamp, $nonce, $token); sort($arr, SORT_STRING); $str = implode($arr); $str = sha1($str); if($str == $signature) { echo $echostr; } } //验证请求是否合法 function checkSignature() { $token = "yourtoken"; $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $arr = array($token, $timestamp, $nonce); sort($arr); $str = sha1(implode($arr)); if ($str == $signature) { return true; } else { return false; } } //消息回复函数 function responseMsg() { //获取微信公众平台POST过来的数据 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)) { //解析POST过来的XML数据包 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); //定义消息类型 $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml>"; if(!empty($keyword)) { $contentStr = "Welcome ".$fromUsername." to our site!"; $msgType = "text"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; } else { echo "Input something..."; } } else { echo ""; exit; } } //主函数 if (checkSignature()) { if (isset($_GET['echostr'])) { getToken(); exit; } else { responseMsg(); } } else { echo ""; exit; } ?>
5. Deploy the code to the server and WeChat public platform
Upload the written code to your own server, and Just configure it on the WeChat public platform.
6. Summary
Through the introduction of this article, we can learn how to build a WeChat public account application through PHP code. In the actual operation process, you also need to pay attention to some details, such as testing the application, updating the application in time, etc. Master these skills and I believe you can also become an excellent WeChat official account developer!
The above is the detailed content of WeChat public account building php code. For more information, please follow other related articles on the PHP Chinese website!