위 챗 공중 번호 개발 음성 메시지 인식 phop 코드
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;
}
}
}
?>
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Laravel - 변환된 유효성 검사 규칙으로 API 요청 제공동적 콘텐츠를 위해 API를 통해 Laravel CMS에 연결하는 모바일 앱(또는 웹사이트) 구축을 고려하십시오. 이제 앱은 CMS에서 번역된 콘텐츠를 받을 것으로 예상되는 다국어 앱이 될 수 있습니다. 일반적으로 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.