接入前提
需要到官方商城购买UNI助理训练大脑,拿到api_key和app_id。(费用几十张,一般公司才会需要)
接入步骤
部分参考代码
//获取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