码迷,mamicode.com
首页 > 微信 > 详细

夺命雷公狗---微信开发40----微信语言识别接口2(点歌系统)

时间:2016-03-04 08:10:15      阅读:472      评论:0      收藏:0      [点我收藏+]

标签:

语音识别时候记住一定要加上中文的“!”号噢,否则一定不会成功

点歌系统开工,index.php代码如下所示:

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

//define your token
require_once "common.php";
//这里是引入curl发送函数的类
require_once ‘WeChat.class.php‘;
define("TOKEN", "twgdh");

//这里让这个类继承了curl发送参数的类
class wechatCallbackapiTest extends WeChat
{
    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 */
                // 使用simplexml技术对xml进行解析 
                // libxml_disable_entity_loader(true), 是从安全性考虑,为了防止xml外部注入,
                //只对xml内部实体内容进行解析
                libxml_disable_entity_loader(true);
                //加载 postStr 字符串
                $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->Content);
                $time = time();
                global $tmp_arr;
                //根据接收到的消息类型,来进行分支处理(switch)
                switch($postObj->MsgType)
                {
                    case ‘event‘:
                        if($postObj->Event == ‘subscribe‘)
                        {
                            
                            $contentStr = "欢迎关注leigood微信测试号噢";
                            $resultStr = sprintf($tmp_arr[‘text‘], $fromUsername, $toUsername, $time, $contentStr);
                            echo $resultStr; 
                        }
                        
                        //响应用户的点击事件
                        if($postObj->Event == ‘CLICK‘){
                            if($postObj->EventKey == ‘V1001_TODAY_MUSIC‘){ //自定义菜单里面有很多个,这里key是自定义的
                                //这里主要写自己想要的业务逻辑
                                $contentStr = "夺命雷公狗欢迎您来到编程世界";
                                $resultStr = sprintf($tmp_arr[‘text‘], $fromUsername, $toUsername, $time, $contentStr);
                                echo $resultStr; 
                            }
                        }
                        
                        break;
                    case ‘text‘: //回复文本模块
                        //这里开始测试客服消息接口了
                        if($keyword == ‘文本‘){
                           //获取token
                           require ‘get_token.php‘;
                           $contentStr = ‘这是客服接口的回复,我们可以帮助您什么?‘;
                           //对发送的内容进行urlencode编码,防止中文乱码
                           $contentStr = urlencode($contentStr);
                           //到时候我们我发送的内容我们放到一个数组里面去了
                           $content_arr = array(‘content‘=>"{$contentStr}");
                           //这里的意思是将来我要发送消息给这个用户
                           $reply_arr = array(‘touser‘=>"{$fromUsername}",‘msgtype‘=>‘text‘,‘text‘=>$content_arr);
                           //下一步就是将编码转成规定的json格式
                           $post = json_encode($reply_arr);
                           //url解码,如果不解码他将会发来一段二进制代码
                           $post = urldecode($post);
                           $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$access_token}";
                           //这里我们来使用类的继承机制,来简化代码
                           $this -> http_request($url,$post);
                           //这里是可以回复多条消息的,只需要在执行下这个函数即可
                           //$this -> http_request($url,$post);
                           
                        }else{
                            $contentStr = ‘您输入的格式有误‘;
                            $resultStr = sprintf($tmp_arr[‘text‘], $fromUsername, $toUsername, $time, $contentStr);
                            echo $resultStr;
                        }
                        break;
                    
                    case ‘voice‘;
                        //简单的微信语音点歌系统
                        //暂时我们的歌曲还没放入数据库里面,如果在数据库里面,如果已入库$postObj->Recognition在数据库查
                        //这后面的!号一定要加,否则查询失败
                        if($postObj->Recognition ==‘十八摸!‘){
                            $title = ‘十八摸‘; //标题
                            $Description = ‘十八摸,您懂得~~~~~‘; //音乐描述
                            $MusicUrl = "http://weixin.showtp.com/music/qbm.mp3"; //音乐链接
                            $HQMusicUrl = "http://weixin.showtp.com/music/sbm.mp3"; //高质量音乐链接
                            
                            $resultStr = sprintf($tmp_arr[‘music‘], $fromUsername, $toUsername, $time, $title,$Description,$MusicUrl,$HQMusicUrl);
                            echo $resultStr;
                            
                        }else if($postObj->Recognition ==‘强暴神曲!‘){
                            $title = ‘强暴神曲!‘; //标题
                            $Description = ‘强暴的艺术歌曲,您懂得~~~~~‘; //音乐描述
                            $MusicUrl = "http://weixin.showtp.com/music/qbsq.mp3"; //音乐链接
                            $HQMusicUrl = "http://weixin.showtp.com/music/qbsq.mp3"; //高质量音乐链接
                            
                            $resultStr = sprintf($tmp_arr[‘music‘], $fromUsername, $toUsername, $time, $title,$Description,$MusicUrl,$HQMusicUrl);
                            echo $resultStr;
                            
                        }else if($postObj->Recognition ==‘喜欢你!‘){
                            $title = ‘喜欢你!‘; //标题
                            $Description = ‘喜欢你,您懂得~~~~~‘; //音乐描述
                            $MusicUrl = "http://weixin.showtp.com/music/xhn.mp3"; //音乐链接
                            $HQMusicUrl = "http://weixin.showtp.com/music/xhn.mp3"; //高质量音乐链接
                            
                            $resultStr = sprintf($tmp_arr[‘music‘], $fromUsername, $toUsername, $time, $title,$Description,$MusicUrl,$HQMusicUrl);
                            echo $resultStr;
                            
                        }else{
                            $contentStr = ‘抱歉您所点的歌曲在leigood数据库里面没有找到‘;
                            $resultStr = sprintf($tmp_arr[‘text‘], $fromUsername, $toUsername, $time, $contentStr);
                            echo $resultStr;
                        }
                    break;
                }
        }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;
        }
    }
}

//如果这段代码放在上面,那程序将会报错,因为继承的问题,会显示类没有找到
$wechatObj = new wechatCallbackapiTest();
//当接入成功后,请注销这句话,否则,会反复验证。
//$wechatObj->valid();
//添加响应请求的语句
$wechatObj->responseMsg();

?>

 

 

核心代码如下所示:

技术分享

 

然后就拿着您的手机说出您所给出的歌名,他即可返回您所点的那首歌曲。

夺命雷公狗---微信开发40----微信语言识别接口2(点歌系统)

标签:

原文地址:http://www.cnblogs.com/leigood/p/5240778.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!