


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

AI Hentai Generator
Generate AI Hentai for free.

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

1. WeChat is a social platform that pays attention to privacy protection. Users cannot see who has visited their Moments or personal homepage. 2. This design is intended to protect user privacy and avoid potential harassment or snooping. 3. Users can only see the likes and comments records in their circle of friends, further ensuring the confidentiality of personal information.

Thanks to netizens Qing Qiechensi, HH_KK, Satomi Ishihara and Wu Yanzu of South China for submitting clues! According to news on September 2, there are recent rumors that "iPhone 16 may not support WeChat." In response to this, a reporter from Shell Finance called Apple's official hotline. Apple's technical consultant in China responded that whether iOS systems or Apple devices can continue to use WeChat, and WeChat The issue of whether it can continue to be listed and downloaded on the Apple App Store requires communication and discussion between Apple and Tencent to determine the future situation. Software App Store and WeChat Problem Description Software App Store technical consultant pointed out that developers may need to pay fees to put software on the Apple Store. After reaching a certain number of downloads, Apple will need to pay corresponding fees for subsequent downloads. Apple is actively communicating with Tencent,

DeepSeek: A powerful AI image generation tool! DeepSeek itself is not an image generation tool, but its powerful core technology provides underlying support for many AI painting tools. Want to know how to use DeepSeek to generate images indirectly? Please continue reading! Generate images with DeepSeek-based AI tools: The following steps will guide you to use these tools: Launch the AI Painting Tool: Search and open a DeepSeek-based AI Painting Tool (for example, search "Simple AI"). Select the drawing mode: select "AI Drawing" or similar function, and select the image type according to your needs, such as "Anime Avatar", "Landscape"

1. On the old device, click "Me" → "Settings" → "Chat" → "Chat History Migration and Backup" → "Migrate". 2. Select the target platform device to be migrated, select the chat records to be migrated, and click "Start". 3. Log in with the same WeChat account on the new device and scan the QR code to start chat record migration.

Rumors of WeChat supporting iPhone 16 were debunked. Thanks to netizens Xi Chuang Jiu Shi and HH_KK for submitting clues! According to news on September 2, there are rumors today that WeChat may not support iPhone 16. Once the iPhone is upgraded to the iOS 18.2 system, it will not be able to use WeChat. According to "Daily Economic News", it was learned from people familiar with the matter that this rumor is a rumor. Apple's response: According to Shell Finance, Apple's technical consultant in China responded that the issue of whether WeChat can continue to be used on iOS systems or Apple devices, and whether WeChat can continue to be listed and downloaded in the Apple App Store, needs to be resolved between Apple and Tencent. Only through communication and discussion can we determine the future situation. Currently, Apple is actively communicating with Tencent to confirm whether Tencent will continue to

What is GateToken(GT) currency? GT (GateToken) is the native asset on the GateChain chain and the official platform currency of Gate.io. The value of GT coins is closely related to the development of Gate.io and GateChain ecology. What is GateChain? GateChain was born in 2018 and is a new generation of high-performance public chain launched by Gate.io. GateChain focuses on protecting the security of users' on-chain assets and providing convenient decentralized transaction services. GateChain's goal is to build an enterprise-level secure and efficient decentralized digital asset storage, distribution and transaction ecosystem. Gatechain has original

1. Avoid frequently publishing advertising content, such as continuous promotion of products or discounts. 2. Use marketing or sensitive words with caution, such as [order], [purchase], etc. 3. Reasonably control the release frequency and avoid frequent updates, at least half an hour apart. 4. Present the copy in paragraphs, with each paragraph not exceeding 100 words, and the length controlled to be within 6 lines and within 200 words. 5. Avoid copying and pasting the same copy, and ensure that the published content is original or appropriately adapted.

1. Open the WeChat mini program and enter the corresponding mini program page. 2. Find the member-related entrance on the mini program page. Usually the member entrance is in the bottom navigation bar or personal center. 3. Click the membership portal to enter the membership application page. 4. On the membership application page, fill in relevant information, such as mobile phone number, name, etc. After completing the information, submit the application. 5. The mini program will review the membership application. After passing the review, the user can become a member of the WeChat mini program. 6. As a member, users will enjoy more membership rights, such as points, coupons, member-exclusive activities, etc.
