WeChat public account development-speech message recognition php code, public php

Source: Internet
Author: User

Public Account development-speech message recognition php code, public php

This example shares the php speech message recognition code for your reference. The specific content is as follows:

1. Enable Speech Recognition (disabled by default)

2. Speech Recognition

Note: After activating voice Recognition, each time a user sends a voice to the public account, a Recognition field is added to the XML data packet of the pushed voice message (Note: Due to the client cache, developers can enable or disable the speech recognition function. This function takes effect immediately for new users and takes effect 24 hours for users who are already paying attention to it. Developers can review this account for testing ). The XML voice data packet after enabling speech recognition is as follows:

<? Php/*** wechat php test * // define your tokendefine ("TOKEN", "weixin"); $ wechatObj = new wechatCallbackapiTest (); // $ wechatObj-> valid (); // Interface Verification $ wechatObj-> responseMsg (); // call the reply message method class wechatCallbackapiTest {public function valid () {$ echoStr = $ _ GET ["echostr"]; // valid signature, option if ($ this-> checkSignature () {echo $ echoStr; exit ;}} public function responseMsg () {// get post data, May be due To the different environments $ postStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; // extract post data if (! Empty ($ postStr) {/* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection, the best way is to check the validity of xml by yourself */libxml_disable_entity_loader (true ); $ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA); $ fromUsername = $ postObj-> FromUserName; $ toUsername = $ postObj-> ToUserName; $ keyword = trim ($ postObj-> Content); $ time = time (); $ m SgType = $ postObj-> MsgType; // Message Type $ event = $ postObj-> Event; // time type, subscribe, unsubscribe) $ 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> "; switch ($ msgType) {case" event ": if ($ event = "subscribe") {$ contentStr = "Hi, please pay attention to haixian department store! ". "\ N ". "reply to the number '1' to learn about the store address. ". "\ n ". "reply to the number '2' to learn about the product type. ";} break; case" text ": // text message switch ($ keyword) {case" 1 ": $ contentStr =" shop address :". "\ n ". "jianggan district, Hangzhou City. "; break; case" 2 ": $ contentStr =" item type :". "\ n ". "cup, bowl, cotton swab, bucket, waste bin, towel (Brush), mop, broom ,". "hangers, hooks, toothpicks, garbage bags, fresh-keeping bags (film), scissors, fruit knives, lunch boxes, etc. "; break; default: $ contentStr =" sorry, I will reply to your content later ";} break; case" voice ": // voice message // Speech recognition $ Recognition = $ postObj-> recognition; $ format = $ postObj-> Forma T; $ contentStr = "the voice message you sent ". "\ n ". "Voice format :". "\ n ". $ format. "\ n ". "Voice content :". "\ n ". $ recognition; break;} $ msgType = "text"; $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr ); echo $ resultStr;} else {echo ""; exit ;}} private function checkSignature () {// you must define TOKEN by yourself if (! Defined ("TOKEN") {throw new Exception ('token is not defined! ');} $ Signature = $ _ GET ["signature"]; $ timestamp = $ _ GET ["timestamp"]; $ nonce = $ _ GET ["nonce"]; $ token = TOKEN; $ tmpArr = array ($ token, $ timestamp, $ nonce); // use SORT_STRING rule sort ($ tmpArr, SORT_STRING ); $ tmpStr = implode ($ tmpArr); $ tmpStr = sha1 ($ tmpStr); if ($ tmpStr ==$ signature) {return true ;} else {return false ;}}}?>

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.