Development of micro-credit public number voice message recognition PHP code _php instance

Source: Internet
Author: User
Tags cdata

This example for you to share the PHP micro-letter speech message Recognition code for your reference, the specific contents are as follows

1. Open speech recognition (closed by default)

2. Speech recognition

Note that after the speech recognition is opened, the micro-letter adds a recognition field to the tweets message XML packet when the user sends the voice to the public number (note: Because of the client cache, the developer turns on or off the speech recognition function, it immediately takes effect for new followers, It takes 24 hours to take effect on the user who has been concerned. Developers can focus on this account again for testing). The speech XML packets that turn on speech recognition are as follows:

<?php/** * WeChat PHP Test * *//define Your token ("define", "token");
$WECHATOBJ = new Wechatcallbackapitest (); $WECHATOBJ->valid ();//interface Validation $WECHATOBJ->responsemsg ()//Call Reply message method class Wechatcallbackapitest {public

  function valid () {$echoStr = $_get["Echostr"];
   Valid signature, option if ($this->checksignature ()) {echo $echoStr;
  Exit The Public Function responsemsg () {//get post data, May is 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
     On, the best way are 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 ();   $msgType = $POSTOBJ->msgtype;//message type $event = $POSTOBJ->event;//Time type, subscribe (subscription), unsubscribe (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 the Sea Fairy daily necessities!". \ n "." Reply Number ' 1 ' to find out shop address. "." \ n "." 
     Reply to the number ' 2 ' to understand the type of product. ";}
     Break Case "text"://Text message switch ($keyword) {case "1": $contentStr = "store Address:". " \ n "." 
       Hangzhou City Jianggan District. "
       Break Case "2": $contentStr = "type of commodity:". \ n "." Cups, bowls, cotton swabs, buckets, garbage cans, dishwashing towel (brush), mop, Broom, "."
       Hangers, sticky hooks, toothpicks, garbage bags, fresh bags (film), scissors, fruit knives, lunch boxes and so on. ";
       Break Default: $contentStr = "toNo, your content I will reply later ";
     } break;
     Case "Voice"://Voice message//speech recognition $recognition = $POSTOBJ->recognition;
     $format = $POSTOBJ->format; $CONTENTSTR = "You are sending a voice message." \ n "." The speech format is: "." \ n ". $format." \ n "." The Voice content is: "."
     \ 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 n
  EW Exception (' TOKEN is not defined! ');
  } $signature = $_get["signature"];
  $timestamp = $_get["timestamp"];
    
  $nonce = $_get["nonce"];
  $token = token;
  $TMPARR = Array ($token, $timestamp, $nonce);
  Use sort_string the rule SORT ($TMPARR, sort_string);
  $TMPSTR = implode ($TMPARR);
  
  $TMPSTR = SHA1 ($TMPSTR);
  if ($tmpStr = = $signature) {return true;
  }else{return false;

 }}}?>

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.