简体   繁体   中英

integrating ibm watson assistant using php

I need to integrate ibm watson chat bot to my website using php and javascript .can any one could help me out of this?

my code:

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

response i am getting is

"Found. Redirecting to https:\/\/watson-api-explorer.ng.bluemix.net\/conversation\/api\/v1\/workspaces\/xxx\/message"

Your URL is wrong. You are pointing at the API Explorer, which used to host the swagger page for the url. Your url should be something like:

https://gateway.watsonplatform.net/assistant/api

For usage check the cURL API Documentation for the service - https://cloud.ibm.com/apidocs/assistant-v2

The actual endpoint you use will depend on the region in which you create your Watson Assistant service. You do need to instantiate an instance in order to be able to use it. When you do instantiate an instance you will be given a url endpoint and an API Key to use. For documentation on the process go to https://cloud.ibm.com/docs/services/assistant?topic=assistant-getting-started#getting-started

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM