标签:
微信语义理解接口称为智能接口,但微信只是开了一半接口,剩下一半要求用户自己去对接业务。这个语义理解类似中文分词技术,优于模糊匹配,拿查股票为例,你说“东风汽车”,“东风汽车股票”,“东风汽车价格”,“查一查东风汽车当前价”返回的内容是一样的,因此语义理解和微信语音识别的配合是最好的,由于口语化,识别率问题,智能接口就有一定用武之地。查询的参数包括query(语音识别结果),city(股票不影响),category(stock),appid,uid(可填用户openid或其他)。返回Josn格式,主要处理semantic字段,包含股票代码和上股还是深股,返回后再次调用百度api查询当前股价。
<?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 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $type=$postObj->MsgType; $Recognition = $postObj->Recognition; $Recognition=urlencode($Recognition); $time = time(); include ("token.php");//token文件 $c=array("query"=>$Recognition,"city"=>"北京","category"=>"stock","appid"=>"wxee2a90c9b05af7a2","uid"=>"123456"); $post=json_encode($c); $post=urldecode($post); //语义理解 $url = "https://api.weixin.qq.com/semantic/semproxy/search?access_token={$token}"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url);//url curl_setopt($ch, CURLOPT_POST, 1); //post curl_setopt($ch, CURLOPT_POSTFIELDS, $post); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $a = curl_exec($ch); $strjson=json_decode($a); $code = $strjson->semantic->details->code; $category = $strjson->semantic->details->category; //股票查询 $url="http://apistore.baidu.com/microservice/stock?stockid=".$category.$code; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $a = curl_exec($ch); $strjson=json_decode($a); $name=$strjson->retData->stockinfo->name; $price=$strjson->retData->stockinfo->currentPrice; $reply= $name."当前股价".$price; $textTpl = "<xml> <ToUserName>$fromUsername</ToUserName> <FromUserName>$toUsername</FromUserName> <CreateTime>$time</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[$reply]]></Content> <FuncFlag>0</FuncFlag> </xml>"; echo $textTpl; } private function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } } ?>
标签:
原文地址:http://blog.csdn.net/u011330225/article/details/43164145