zoukankan      html  css  js  c++  java
  • 微信开放平台开发(1) 语义理解

    关键字:微信公众平台 微信开放平台 语义理解 semantic 
    作者:方倍工作室 
    原文:http://www.cnblogs.com/txw1958/p/weixin-semantic-analysis.html 

    微信开放平台语义理解接口调用(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_credential
    appid 应用的appid
    secret 应用的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获取到的token
    query String 输入文本串
    category String 需要使用的服务类型,多个用“,”隔开,不能为空
    latitude 见接口协议文档 Float 纬度坐标,与经度同时传入;与城市二选一传入
    longitude 见接口协议文档 Float 经度坐标,与纬度同时传入;与城市二选一传入
    city 见接口协议文档 String 城市名称,与经纬度二选一传入
    region 见接口协议文档 String 区域名称,在城市存在的情况下可省;与经纬度二选一传入
    appid String Appid,开发者的唯一标识
    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."
    ", 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 

  • 相关阅读:
    对于Sobel算子的学习
    HDU 2594(求最长公共前后缀 kmp)
    HDU 6108(整除判断 数学)
    HDU 5968(异或计算 暴力)
    HDU 5963(游戏 博弈+规律)
    简单算法考题记录
    flex与bison
    C++ 智能指针
    Linux 添加设备驱动程序
    Linux 添加系统调用
  • 原文地址:https://www.cnblogs.com/txw1958/p/weixin-semantic-analysis.html
Copyright © 2011-2022 走看看