위 챗 공중 번호 개발 음성 메시지 인식 phop 코드

본 고의 실례 는 여러분 에 게 php 위 챗 음성 메시지 식별 코드 를 공 유 했 습 니 다.여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
1.음성 인식 개통(기본 종료)

2.음성 인식
음성 인식 이 개 통 된 후에 사용자 가 대중 번호 에 음성 을 보 낼 때마다 위 챗 은 추 송 된 음성 메시지 XML 패 킷 에 Recognition 필드 를 추가 합 니 다(비고:클 라 이언 트 캐 시 로 인해 개발 자가 음성 인식 기능 을 켜 거나 닫 으 면 새로운 관심 자 에 게 즉시 효력 이 발생 하고 관심 있 는 사용자 에 게 24 시간 이 걸 립 니 다.개발 자 는 이 계 정 에 다시 관심 을 가지 고 테스트 할 수 있 습 니 다.음성 인식 후 음성 XML 패 키 지 를 켜 면 다음 과 같 습 니 다.


<?php
/**
 * wechat php test
 */

//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();//    
$wechatObj->responseMsg();//        
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();
    $msgType = $postObj->MsgType;//    
    $event = $postObj->Event;//    ,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,          !"."
"." '1', ."."
"." '2', ."; } break; case "text":// switch($keyword){ case "1": $contentStr = " :"."
"." ."; break; case "2": $contentStr = " :"."
"." 、 、 、 、 、 ( )、 、 、" ." 、 、 、 、 ( )、 、 、 ."; break; default: $contentStr = " , "; } break; case "voice":// // $recognition = $postObj->Recognition; $format = $postObj->Format; $contentStr = " "."
"." :"."
".$format."
"." :"."
".$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; } } } ?>
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기