# 申請百度UNIT賬號
## 創建機器人

## 添加技能

# API接入
## 獲取API Key / Secret Key

## //獲取 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;
~~~
```