


WeChat public platform development (4) Simple reply function development_PHP tutorial
The WeChat public platform can make judgments based on the information sent by users and then give corresponding replies, which has good interactivity. The following will simulate a simple reply function. Based on this case, developers can also basically understand the principles of WeChat interaction and conduct deeper development.
We can extract keywords from text messages sent by users through simple if...elseif...else....
The key code is as follows:
<span if</span>(<span $keyword</span>=="你好"<span ){ </span><span $contentStr</span> = "hello"<span ; }</span><span elseif</span>(<span $keyword</span>=="苏州"<span ){ </span><span $contentStr</span> = "上有天堂,下有苏杭"<span ; }</span><span else</span><span { </span><span $contentStr</span> = "感谢您关注【卓锦苏州】 微信号:zhuojinsz"<span ; }</span>
If the user sends "Hello", it will reply "hello". If the user sends "Suzhou", it will reply "There is heaven above, there is Suzhou and Hangzhou below". For other information, it will reply with your welcome message.
<?<span php </span><span /*</span><span * * wechat php test </span><span */</span> <span //</span><span define your token</span> <span define</span>("TOKEN", "zhuojin"<span ); </span><span $wechatObj</span> = <span new</span><span wechatCallbackapiTest(); </span><span $wechatObj</span>-><span responseMsg(); </span><span //</span><span $wechatObj->valid();</span> <span class</span><span wechatCallbackapiTest { </span><span /*</span><span public function valid() { $echoStr = $_GET["echostr"]; //valid signature , option if($this->checkSignature()){ echo $echoStr; exit; } }</span><span */</span> <span public</span> <span function</span><span responseMsg() { </span><span //</span><span get post data, May be due to the different environments</span> <span $postStr</span> = <span $GLOBALS</span>["HTTP_RAW_POST_DATA"<span ]; </span><span //</span><span extract post data</span> <span if</span> (!<span empty</span>(<span $postStr</span><span )){ </span><span $postObj</span> = <span simplexml_load_string</span>(<span $postStr</span>, 'SimpleXMLElement',<span LIBXML_NOCDATA); </span><span $RX_TYPE</span> = <span trim</span>(<span $postObj</span>-><span MsgType); </span><span switch</span>(<span $RX_TYPE</span><span ) { </span><span case</span> "text": <span $resultStr</span> = <span $this</span>->handleText(<span $postObj</span><span ); </span><span break</span><span ; </span><span case</span> "event": <span $resultStr</span> = <span $this</span>->handleEvent(<span $postObj</span><span ); </span><span break</span><span ; </span><span default</span>: <span $resultStr</span> = "Unknow msg type: ".<span $RX_TYPE</span><span ; </span><span break</span><span ; } </span><span echo</span> <span $resultStr</span><span ; }</span><span else</span><span { </span><span echo</span> ""<span ; </span><span exit</span><span ; } } </span><span public</span> <span function</span> handleText(<span $postObj</span><span ) { </span><span $fromUsername</span> = <span $postObj</span>-><span FromUserName; </span><span $toUsername</span> = <span $postObj</span>-><span ToUserName; </span><span $keyword</span> = <span trim</span>(<span $postObj</span>-><span Content); </span><span $time</span> = <span time</span><span (); </span><span $textTpl</span> = "<span <xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml></span>"<span ; </span><span if</span>(!<span empty</span>( <span $keyword</span><span )) { </span><span $msgType</span> = "text"<span ; </span><span if</span>(<span $keyword</span>=="你好"<span ){ </span><span $contentStr</span> = "hello"<span ; }</span><span elseif</span>(<span $keyword</span>=="苏州"<span ){ </span><span $contentStr</span> = "上有天堂,下有苏杭"<span ; }</span><span else</span><span { </span><span $contentStr</span> = "感谢您关注【卓锦苏州】 微信号:zhuojinsz"<span ; } </span><span $resultStr</span> = <span sprintf</span>(<span $textTpl</span>, <span $fromUsername</span>, <span $toUsername</span>, <span $time</span>, <span $msgType</span>, <span $contentStr</span><span ); </span><span echo</span> <span $resultStr</span><span ; }</span><span else</span><span { </span><span echo</span> "Input something..."<span ; } } </span><span public</span> <span function</span> handleEvent(<span $object</span><span ) { </span><span $contentStr</span> = ""<span ; </span><span switch</span> (<span $object</span>-><span Event) { </span><span case</span> "subscribe": <span $contentStr</span> = "感谢您关注【卓锦苏州】"."\n"."微信号:zhuojinsz"."\n"."卓越锦绣,名城苏州,我们为您提供苏州本地生活指南,苏州相关信息查询,做最好的苏州微信平台。"."\n"."目前平台功能如下:"."\n"."【1】 查天气,如输入:苏州天气"."\n"."【2】 查公交,如输入:苏州公交178"."\n"."【3】 翻译,如输入:翻译I love you"."\n"."【4】 苏州信息查询,如输入:苏州观前街"."\n"."更多内容,敬请期待..."<span ; </span><span break</span><span ; </span><span default</span> : <span $contentStr</span> = "Unknow Event: ".<span $object</span>-><span Event; </span><span break</span><span ; } </span><span $resultStr</span> = <span $this</span>->responseText(<span $object</span>, <span $contentStr</span><span ); </span><span return</span> <span $resultStr</span><span ; } </span><span public</span> <span function</span> responseText(<span $object</span>, <span $content</span>, <span $flag</span>=0<span ) { </span><span $textTpl</span> = "<span <xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>%d</FuncFlag> </xml></span>"<span ; </span><span $resultStr</span> = <span sprintf</span>(<span $textTpl</span>, <span $object</span>->FromUserName, <span $object</span>->ToUserName, <span time</span>(), <span $content</span>, <span $flag</span><span ); </span><span return</span> <span $resultStr</span><span ; } </span><span private</span> <span function</span><span checkSignature() { </span><span $signature</span> = <span $_GET</span>["signature"<span ]; </span><span $timestamp</span> = <span $_GET</span>["timestamp"<span ]; </span><span $nonce</span> = <span $_GET</span>["nonce"<span ]; </span><span $token</span> =<span TOKEN; </span><span $tmpArr</span> = <span array</span>(<span $token</span>, <span $timestamp</span>, <span $nonce</span><span ); </span><span sort</span>(<span $tmpArr</span><span ); </span><span $tmpStr</span> = <span implode</span>( <span $tmpArr</span><span ); </span><span $tmpStr</span> = <span sha1</span>( <span $tmpStr</span><span ); </span><span if</span>( <span $tmpStr</span> == <span $signature</span><span ){ </span><span return</span> <span true</span><span ; }</span><span else</span><span { </span><span return</span> <span false</span><span ; } } } </span>?>
Please follow Zhuojin Suzhou WeChat public account, Zhuojin Suzhou is developed based on SAE platform and is targeted at mainstream WeChat functions Conduct development testing.
You can follow the Zhuojin Suzhou public account to conduct functional testing and obtain new application development.
1. Log in to the WeChat client, friends -> Add friends -> Search number -> zhuojinsz, find and follow.
2. Scan the QR code:
We Believe, Great People Share Knowledge...

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





This article provides a detailed guide to safe download of Ouyi OKX App in China. Due to restrictions on domestic app stores, users are advised to download the App through the official website of Ouyi OKX, or use the QR code provided by the official website to scan and download. During the download process, be sure to verify the official website address, check the application permissions, perform a security scan after installation, and enable two-factor verification. During use, please abide by local laws and regulations, use a safe network environment, protect account security, be vigilant against fraud, and invest rationally. This article is for reference only and does not constitute investment advice. Digital asset transactions are at your own risk.

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

Compatibility issues and troubleshooting methods for company security software and application. Many companies will install security software in order to ensure intranet security. However, security software sometimes...

The choice of H5 and applet depends on the requirements. For applications with cross-platform, rapid development and high scalability, choose H5; for applications with native experience, rich functions and platform dependencies, choose applets.

H5 is more flexible and customizable, but requires skilled technology; mini programs are quick to get started and easy to maintain, but are limited by the WeChat framework.

PowerToys is a free collection of tools launched by Microsoft to enhance productivity and system control for Windows users. It provides features through standalone modules such as FancyZones management window layout and PowerRename batch renaming files, making user workflow smoother.

This article provides a brief guide to buying and selling of Binance virtual currency updated in 2025, and explains in detail the operation steps of virtual currency transactions on the Binance platform. The guide covers fiat currency purchase USDT, currency transaction purchase of other currencies (such as BTC), and selling operations, including market trading and limit trading. In addition, the guide also specifically reminds key risks such as payment security and network selection for fiat currency transactions, helping users to conduct Binance transactions safely and efficiently. Through this article, you can quickly master the skills of buying and selling virtual currencies on the Binance platform and reduce transaction risks.

Lianyungang Huaguoshan Scenic Area joins hands with Tencent Cloud to launch the first "dual-core brain" Digital Homo sapiens in the cultural and tourism industry - Monkey King! On March 1, the scenic spot officially connected the Monkey King to the DeepSeek platform, so that it has the two AI model capabilities of Tencent Hunyuan and DeepSeek, bringing tourists a smarter and more considerate service experience. Huaguoshan Scenic Area has previously launched the Monkey King of the Digital Homo sapiens based on Tencent Hunyuan model. This time, Tencent Cloud further utilizes technologies such as the big model knowledge engine to connect it to DeepSeek to achieve a "dual-core" upgrade. This makes the Monkey King's interactive ability to a higher level, faster response speed, stronger understanding ability, and more warmth. Monkey King has strong natural language processing capabilities and can understand various ways of asking questions from tourists.
