1.需要到圖靈機器人的網址,去注冊一下賬號。網址:http://www.tuling123.com/sso-web/index.html?ReturnURL=http%3A%2F%2Fwww.tuling123.com%2Fmember%2Frobot%2Findex.jhtml
登錄之后可以先申請一個免費的做測試。會得到一個apiKey。
2.暫時使用的是2.0版本。根據api文檔,需要傳參到"http://openapi.tuling123.com/openapi/api/v2";json格式.
額,直接上代碼吧。<?php
/**
* 模擬post進行url請求
* @param string $url
* @param array $post_data
*/
function request_post($url = '', $post_data = '') {
if (empty($url) || empty($post_data)) {
return false;
}
$postUrl = $url;
$curlPost = $post_data;
$ch = curl_init();//初始化curl
curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定網頁
curl_setopt($ch, CURLOPT_HEADER, 0);//設置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求結果為字符串且輸出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch);//運行curl
curl_close($ch);
return $data;
}
$url = 'http://openapi.tuling123.com/openapi/api/v2';
$post_data['userInfo']=array('apiKey'=>'你自己申請的那個apiKey','userId'=>1);
$post_data['perception']=array('inputText'=>array('text'=>'你叫什么名字?'));
$post_data=json_encode($post_data);
$res = request_post($url, $post_data);
print_r($res);
?>