php尝试调用一个图灵机器人

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);

?>

猜你喜欢

转载自www.cnblogs.com/cici1989/p/10554990.html
今日推荐