微信开放平台开发(1) 语义理解

来源:互联网 发布:linux命令删除目录 编辑:程序博客网 时间:2024/05/17 20:25

微信开放平台语义理解接口调用(http请求)简单方便,用户无需掌握语义理解及相关技术,只需根据自己的产品特点,选择相应的服务即可搭建一套智能语义服务。

第一步:创建应用

请到“管理中心”创建应用,点击“创建移动应用”或者“创建网站应用”,填写相关资料,然后将该应用提交审核,只有审核通过的应用才能进行开发。

注册完毕,我们会在 7 个工作日内完成审核工作。审核通过之后,开放平台将分配给该移动应用全局唯一的AppID和AppSecret。

第二步:根据AppID和AppSecret获得access token

调用接口:

http请求方式: GET

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
参数说明:
参数是否必须说明grant_type是获取access_token填写client_credentialappid是应用的appidsecret是应用的appsecret
返回说明:

正常情况下,微信会返回下述JSON数据包。

{"access_token":"ACCESS_TOKEN","expires_in":7200} 

 

参数说明access_token获取到的凭证expires_in凭证有效时间,单位:秒

错误时微信会返回错误码等信息,JSON数据包示例如下(该示例为AppID无效错误):

{"errcode":40013,"errmsg":"invalid appid"} 

 

第三步:使用access token调用语义理解接口

输入说明:

http请求方式: POST(请使用https协议)

https://api.weixin.qq.com/semantic/semproxy/search?access_token=YOUR_ACCESS_TOKEN

 

POST数据格式:JSON
POST数据例子:

复制代码
{"query":"查一下明天从北京到上海的南航机票","city":"北京","category": "flight,hotel","appid":"wxaaaaaaaaaaaaaaaa","uid":"123456"}
复制代码

输入参数说明:

参数是否必须参数类型说明access_token是String根据appid和appsecret获取到的tokenquery是String输入文本串category是String需要使用的服务类型,多个用“,”隔开,不能为空latitude见接口协议文档Float纬度坐标,与经度同时传入;与城市二选一传入longitude见接口协议文档Float经度坐标,与纬度同时传入;与城市二选一传入city见接口协议文档String城市名称,与经纬度二选一传入region见接口协议文档String区域名称,在城市存在的情况下可省;与经纬度二选一传入appid是StringAppid,开发者的唯一标识uid否String用户唯一id(非开发者id),用户区分应用下的不同用户(建议填入用户openid),如果为空,则无法使用上下文理解功能。appid和uid同时存在的情况下,才可以使用上下文理解功能。
返回说明:

正常情况下,微信会返回下述JSON数据包。

 

复制代码
{    "errcode": 0,    "query": "查一下明天从北京到上海的南航机票",    "type": "flight",    "semantic": {        "details": {            "start_loc": {                "type": "LOC_CITY",                "city": "北京市",                "city_simple": "北京",                "loc_ori": "北京"            },            "end_loc": {                "type": "LOC_CITY",                "city": "上海市",                "city_simple": "上海",                "loc_ori": "上海"            },            "start_date": {                "type": "DT_ORI",                "date": "2014-03-05",                "date_ori": "明天"            },            "airline": "中国南方航空公司"        },        "intent": "SEARCH"    }}
复制代码

返回参数说明:

参数是否必须参数类型说明errcode是Int表示请求后的状态query是String用户的输入字符串type是String服务的全局类型id,详见协议文档中垂直服务协议定义semantic是Object语义理解后的结构化标识,各服务不同result否Array部分类别的结果answer否String部分类别的结果html5展示,目前不支持text否String特殊回复说明

更多详细内容与协议说明,请查看:语义理解接口协议文档

代码实现 

复制代码
<?php/*    方倍工作室 http://www.fangbei.org/    CopyRight 2014 All Rights Reserved    微信开放平台接口SDK*//*    require_once('weixin.class.php');    $weixin = new class_weixin();*/define('APPID',         "wxed782be999f86eab");define('APPSECRET',        "72edec63779f7aa16a3a33447e2c7-fb");class class_weixin{    var $appid = APPID;    var $appsecret = APPSECRET;    //构造函数,获取Access Token    public function __construct($appid = NULL, $appsecret = NULL)    {        if($appid && $appsecret){            $this->appid = $appid;            $this->appsecret = $appsecret;        }        //1. 本地写入        $res = file_get_contents('access_token.json');        $result = json_decode($res, true);        $this->expires_time = $result["expires_time"];        $this->access_token = $result["access_token"];        if (time() > ($this->expires_time + 3600)){            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret;            $res = $this->http_request($url);            $result = json_decode($res, true);            $this->access_token = $result["access_token"];            $this->expires_time = time();            file_put_contents('access_token.json', '{"access_token": "'.$this->access_token.'", "expires_time": '.$this->expires_time.'}');        }    }    public function semantic_search($record)    {        $data = urldecode(json_encode($record));        $url = "https://api.weixin.qq.com/semantic/semproxy/search?access_token=".$this->access_token;        $res = $this->http_request($url, $data);        return json_decode($res, true);    }    //HTTP请求(支持HTTP/HTTPS,支持GET/POST)    protected function http_request($url, $data = null)    {        $curl = curl_init();        curl_setopt($curl, CURLOPT_URL, $url);        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);        if (!empty($data)){            curl_setopt($curl, CURLOPT_POST, 1);            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);        }        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);        $output = curl_exec($curl);        curl_close($curl);        return $output;    }    //日志记录    private function logger($log_content)    {        if(isset($_SERVER['HTTP_APPNAME'])){   //SAE            sae_set_display_errors(false);            sae_debug($log_content);            sae_set_display_errors(true);        }else if($_SERVER['REMOTE_ADDR'] != "127.0.0.1"){ //LOCAL            $max_size = 500000;            $log_filename = "log.xml";            if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);}            file_put_contents($log_filename, date('Y-m-d H:i:s').$log_content."\r\n", FILE_APPEND);        }    }}
复制代码

 

接口调用

复制代码
    $weixin = new class_weixin();    $record =  array('query' => "查一下明天从北京到深圳的深航机票",                     'city' => "北京",                     'category' => "flight,hotel",                     'appid' => $weixin->appid,                     'uid' => ""                     );    $result = $weixin->semantic_search($record);
复制代码

结果返回

复制代码
{       "errcode" : 0,       "query" : "查一下明天从北京到深圳的国航机票",       "semantic" : {          "details" : {             "airline" : "中国国际航空公司",             "answer" : "已帮您预定2015-12-19,从北京市出发,前往深圳市的航班。",             "context_info" : {                "isFinished" : "1",                "null_times" : "0"             },             "end_loc" : {                "city" : "深圳市",                "city_simple" : "深圳",                "loc_ori" : "深圳",                "modify_times" : "0",                "province" : "广东省",                "province_simple" : "广东|粤",                "slot_content_type" : "2",                "type" : "LOC_CITY"             },             "hit_str" : "查 一下 明天 从 北京 到 深圳 国航 机票 ",             "start_date" : {                "date" : "2015-12-19",                "date_lunar" : "2015-11-09",                "date_ori" : "明天",                "modify_times" : "0",                "slot_content_type" : "2",                "type" : "DT_ORI",                "week" : "6"             },             "start_loc" : {                "city" : "北京市",                "city_simple" : "北京|京",                "loc_ori" : "北京",                "modify_times" : "0",                "slot_content_type" : "2",                "type" : "LOC_CITY"             }          },          "intent" : "SEARCH"       },       "type" : "flight"    }
复制代码

 

原文:http://www.cnblogs.com/txw1958/p/weixin-semantic-analysis.html 

0 0
原创粉丝点击