# 申请百度UNIT账号
## 创建机器人
![](https://img.kancloud.cn/f1/e6/f1e629e8af7b6459ede2e15141c40fcf_1048x596.png)
## 添加技能
![](https://img.kancloud.cn/dc/d6/dcd65bbd3ceb3d2c58d037597668bccf_1255x481.png)
# API接入
## 获取API Key / Secret Key
![](https://img.kancloud.cn/53/19/531990b8572f803241cf65300473c8e3_1004x595.png)
## //获取 token
```
~~~
// grant\_type:**必须参数,固定为`client_credentials`;
// client\_id:**必须参数,应用的`API Key`;
// client\_secret:**必须参数,应用的`Secret Key`;
$grant_type = 'client_credentials';
$client_id = 'Ut45*************EvyA5';
$client_secret = 'Oi*************M04q';
$url= "https://aip.baidubce.com/oauth/2.0/token? grant_type=$grant_type&client_id=$client_id&client_secret=$client_secret&"
$string = curl($url); 获取到一个json格式对象;一个机器人对应一个固定的 字符串;可外部获取直接写死;
$b = json_decode($string,true); 将此json格式的对象转为数组;方便取值,也可直接对象取值;
$token = $b['access_token'];
~~~
```
## UNIT开发文档链接
[链接](https://ai.baidu.com/ai-doc/UNIT/bkiy75fn9)
## 简单的实现接入
```
~~~
$contene = "你好啊";你发个机器人的文本内容
$bot_id = ""; 机器人的技能ID;
log_id 发送的标识,随意填
$bodys = '{"bot_session":"","log_id":"7aa","request":{"bernard_level":1,"query":"'.$content.'","query_info":{"source":"KEYBOARD","type":"TEXT"},"updates":"","user_id":"0000"},"bot_id":"'.$bot_id .'","version":"2.0"}';
$res = request_post($url,$bodys);
$a= json_decode($res,true);
$contents = "";
机器人回复内容数量不固定,用循环加换行一次性取出
foreach ( $a['result']['response']['action_list'] as $v)
{
$contents.= $v['say']."\n";
}
echo $contents;
~~~
```