1、接收语音识别结果

if($msgType=="voice"){
    //收到语音消息
    //MediaId    语音消息媒体id,可以调用多媒体文件下载接口拉取数据。
    //Format     语音格式,如amr,speex等
    $format = $postObj->Format;
    $mediaId = $postObj->MediaId;
    //开通语音识别功能,用户每次发送语音给公众号时,微信会在推送的语音消息XML数据包中,增加一个Recongnition字段。
    //注:由于客户端缓存,开发者开启或者关闭语音识别功能,对新关注者立刻生效,对已关注用户需要24小时生效。开发者可以重新关注此帐号进行测试。
    $recognition = $postObj->Recognition;
    $contentStr = "Welcome to wechat world! Format ".$format."  MediaId ".$mediaId."  Recognition ".$recognition." ok ";
    $this->responseMsgText($fromUsername, $toUsername, $time,  $contentStr  );
}

2、发送客服消息

       当用户主动发消息给公众号的时候(包括发送信息、点击自定义菜单、订阅事件、扫描二维码事件、支付成功事件、用户维权),微信将会把消息数据推送给开发者,开发者在一段时间内(目前修改为48小时)可以调用客服消息接口,通过POST一个JSON数据包来发送消息给普通用户,在48小时内不限制发送次数。此接口主要用于客服等有人工消息处理环节的功能,方便开发者为用户提供更加优质的服务。

获取u

//发送客服消息
    public function sendMessage(){
        $url="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=";
        $url.=$this->getacctoken();
        //可发送 文本 图片 语音 视频 音乐 图文 模版详见http://mp.weixin.qq.com/wiki/index.php?title=%E5%8F%91%E9%80%81%E5%AE%A2%E6%9C%8D%E6%B6%88%E6%81%AF
        $post_data='
                {
                    "touser":"a openId ",
                    "msgtype":"text",
                    "text":
                    {
                         "content":"Hello World"
                    }
                }
            ';
        $ret = $this->cpost($url,$post_data);
        print_r( $ret );
    }

3、基础 获取access_token

//获取access_token
//查找数据库 发现过期 重新获取 存入数据库
//           有效期内 返回存储的access_token
public function getacctoken(){
    $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APPSECRET;
    $accesstoken = M("accesstoken"); // 实例化wlog对象
    $atoken = $accesstoken ->where('aid=1  ')->find();
    $time = time();
    if($atoken){
        $expires_in=$accesstoken ->where('aid=1')->getField('expires_in');
                      
        if($time>=$expires_in){
            $adata = $this->cget($url);
            echo  "time is = ".$time."access_token = ".$adata['access_token'];
            $data['access_token']= $adata['access_token'];
            $data['expires_in']=$time + $adata['expires_in'];
            $accesstoken->where('aid=1')->save($data);
            return $adata['access_token'];
        }else{
            return  $accesstoken ->where('aid=1')->getField('access_token');
        }
    }else{
        $adata = $this->cget($url);
        echo  "time is = ".$time."access_token = ".$adata['access_token'];
        $data['access_token']= $adata['access_token'];
        $data['expires_in']=$time + $adata['expires_in'];
        $accesstoken->where('aid=1')->save($data);
        return $adata['access_token'];
                  
    }
}