【备忘】狂问接入总结

2023-12-28 18:46:28

接入前提

需要到官方商城购买UNI助理训练大脑,拿到api_key和app_id。(费用几十张,一般公司才会需要)

接入步骤

  1. 新建一个项目
  2. 先获取用户token
  3. 再用token进行提问
  4. 拿到回答结果(保存或输出,看你需求)

部分参考代码


    //获取token
    function gettoken(){
        $url = $this->host.'/kw/user/apiemploys/getusertoken';
        $res = Curl::curl_post($url,[
            'api_key'=>$this->api_key,
        ]);
        $res = json_decode($res['data'], true);
        session('kw_user_token',$res['data']['token']);
        return view();
    }
    //提问
    function que(){
        set_time_limit(30);
        if(request()->isPost()){
            $message = input('message');
            $param = [
                "message" => $message,
                "app_id" => $this->app_id,
            ];
            $url = $this->host.'/kw/user/app/send';
            $header = ["UserToken: ".session('kw_user_token'),"Content-Type:application/json",'Accept: application/json'];
            $result = $this->curlRequest($url,'POST',json_encode($param),$header);
            return json(Tools::set_ok('ok',explode('data:',$result)));
        }
        return view();
    }


    /**
     * 统一请求 GEt请求
     * @param String $url 接口地址
     */
    public function curlRequest($url, $method = 'GET',$data=null,$header=array(),$call_back=null)
    {
        set_time_limit(30);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        if($header){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        if($method = 'POST'){
            if($data) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        if($call_back){
            curl_setopt($ch, CURLOPT_WRITEFUNCTION, $call_back);
        }
        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            return [
                'status' => 'error',
                'message' => 'curl 错误信息: ' . curl_error($ch)
            ];
        }
        curl_close($ch);
        return $result;
    }

结语:功能不难,熟悉熟悉就可以搞了。

END

文章来源:https://blog.csdn.net/qq_15941409/article/details/135275488
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。