In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "PHP Wechat development of how to achieve automatic text reply function", the content is simple and clear, I hope it can help you solve your doubts, now let the editor lead you to study and learn about "PHP Wechat development how to achieve automatic text reply function" this article.
First of all, go to the Wechat public platform to sign up for an account (there are a lot of things to fill in for registration). After registering, log in. You can see the "developer Center" on the left. Before opening the developer Center, you seem to need to improve some information and follow the steps. After entering the developer Center, edit it first.
When modifying the configuration, note:
URL is the PHP script under your own domain name (read down the demo with the script), which is used to interface with Wechat. Like http://www.example.com/weixin.php.
Token is a constant defined in the above script, such as in your PHP script:
Define ("TOKEN", "my_weixin")
So, when you fill in Token, you fill in abcdefgh.
EncodingAESKey is used for message encryption. You can write a combination of 43 numbers and letters yourself, or you can choose "random generation", usually random generation.
After filling it out, save it (if Token verification fails when you save, make sure that token is consistent and try saving it a few more times).
After you have saved it, click "Open" next to the modified configuration.
Then you can edit your PHP script. (if you don't have your own domain name, you can use Sina Cloud's free SAE, and you'd better complete your real name verification.)
The script for demo is as follows: follow the public platform (after Subscription account, for example), and the function is: enter hehe and return hello worldviews! If you enter other characters, return to enter heeh.
Header ('content-type:text/html;charset=utf-8'); define ("TOKEN", "my_weixin"); / / define your token$wx = new wechatCallbackapiTest (); if ($_ GET [' echostr']) {$wx- > valid (); / / verify else {$wx- > responseMsg () if echostr is sent; / / if there is no echostr, return the message} class wechatCallbackapiTest {public function valid () {/ / valid signature, option $echoStr = $_ GET ["echostr"] If ($this- > checkSignature ()) {/ / call the verification field echo $echoStr; exit;}} public function responseMsg () {/ / get post data, May be due to the different environments $postStr = $GLOBALS ["HTTP_RAW_POST_DATA"] / / receive XML data from Wechat / / extract post data if (! empty ($postStr)) {/ / parse XML from post as an object $postObj $postObj = simplexml_load_string ($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj- > FromUserName; / / user requesting message $toUsername = $postObj- > ToUserName; / / official account id $keyword = trim ($postObj- > Content) of "I"; / / message content $time = time () / / timestamp $msgtype = 'text'; / / message type: text $textTpl = "% s"; if ($keyword = =' hehe') {$contentStr = 'hello worldworldly qualified messages'; $resultStr = sprintf ($textTpl, $fromUsername, $toUsername, $time, $msgtype, $contentStr); echo $resultStr; exit ();} else {$contentStr = 'enter hehe try'; $resultStr = sprintf ($textTpl, $fromUsername, $toUsername, $time, $msgtype, $contentStr) Echo $resultStr; exit ();} else {echo "; exit;}} / / Verification fields private function checkSignature () {$signature = $_ GET [" signature "]; $timestamp = $_ GET [" timestamp "]; $nonce = $_ GET [" nonce "]; $token = TOKEN; $tmpArr = array ($token, $timestamp, $nonce); sort ($tmpArr); $tmpStr = implode ($tmpArr); $tmpStr = sha1 ($tmpStr); if ($tmpStr = $signature) {return true } else {return false;}
If you send a message, the system prompts: the public platform cannot mention co-service for the time being, please try again later. Then there is probably something wrong with the code syntax. Check the syntax errors and try again.
Attached:
When new users follow your official account, automatically return a message: (add this code before judging $keyword).
If ($postObj- > MsgType = = 'event') {/ / if the message type in the XML message is event if ($postObj- > Event = =' subscribe') {/ / if it is a subscription event $contentStr = "Welcome to subscribe misaka last summer! \ nMore wonderful content: http://www.php.cn/"; $resultStr = sprintf ($textTpl, $fromUsername, $toUsername, $time, $msgtype, $contentStr); echo $resultStr; exit ();}} these are all the contents of the article "how to achieve automatic text reply in the Development of PHP Wechat". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.