我需要使用php和javascript将ibm watson聊天机器人集成到我的网站。任何人都可以帮助我吗?
我的代码:
<?php
if(isset($_POST['message'])){
// Unique identifier of the workspace.
$workspace_id = 'xxxx';
// Release date of the API version in YYYY-MM-DD format.
$release_date = '2019-05-04';
// Username of a user for the service credentials.
$username = 'yyyyy';
// Password of a user for the service credentials.
$password = 'zzzzz';
// Make a request message for Watson API in json.
$data['input']['text'] = $_POST['message'];
if(isset($_POST['context']) && $_POST['context']){
$data['context'] = json_decode($_POST['context'], JSON_UNESCAPED_UNICODE);
}
$data['alternate_intents'] = false;
$json = json_encode($data, JSON_UNESCAPED_UNICODE);
// Post the json to the Watson API via cURL.
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'https://watson-api-explorer.mybluemix.net/conversation/api/v1/workspaces/'.$workspace_id.'/message');
curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$result = trim( curl_exec( $ch ) );
curl_close($ch);
// Responce the result.
echo json_encode($result, JSON_UNESCAPED_UNICODE);
}
我得到的答复是
"Found. Redirecting to https:\/\/watson-api-explorer.ng.bluemix.net\/conversation\/api\/v1\/workspaces\/xxx\/message"
答案 0 :(得分:0)
您的URL错误。您所指向的是API Explorer,用于托管网址的大页面。您的网址应类似于:
https://gateway.watsonplatform.net/assistant/api
有关使用情况,请查看该服务的cURL API文档-https://cloud.ibm.com/apidocs/assistant-v2
您使用的实际端点将取决于您创建Watson Assistant服务的区域。您确实需要实例化实例才能使用它。实例化实例时,将为您提供url端点和要使用的API密钥。有关该过程的文档,请访问https://cloud.ibm.com/docs/services/assistant?topic=assistant-getting-started#getting-started