Watson Concept Extension Service

I am trying to use Watson concept extension service in php.
I am using the following code to load a list of seeds -

 <?php
header('Content-type: application/json');  

$services_json = json_decode(getenv('VCAP_SERVICES'), true);  
$cred = $services_json["concept_expansion"][0]["credentials"];  
// credentials
$username = $cred["username"];  
$password = $cred["password"];  
$url = $cred["url"] . '/v1/upload';  
$auth = base64_encode($username . ":" . $password);  

try {  
    //List of terms to seed the concept expansion.
    $temp  = array('seeds' => array('motrin','aspirin','keflex' ) );  
    $data = array(  
     'seeds' => $temp,  
     'dataset' => 'mtsamples', 
     'label' => 'drugs'   // label for the seeds
    );    
    $data_string = json_encode($data);                                                                                       
    $curl = curl_init();  

   curl_setopt($curl, CURLOPT_HTTPHEADER, array(                                                                            
   'Content-Type: application/json',  
   'X-synctimeout: 30',  
   'Authorization: Basic ' . $auth)                                                                         
   );       

   curl_setopt($curl, CURLOPT_POST, true);         
   curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);  
   curl_setopt($curl, CURLOPT_URL, $url);  
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  
   $result = curl_exec($curl);  
   curl_close($curl);  
   echo $result;  
  } catch(Exception $e) {  
        echo $e->getMessage();
      }  
?>  

      

But the code gives error 400. Is there something I am missing?

+3


source to share


1 answer


I am no php, but it looks like you are trying to execute curl command, so I will give you curl command :)

curl -X POST \
  -u username:password \
  -d "{\"dataset\": \"mtsamples\", \"seeds\": [\"motrin\", \"tylenol\", \"aspirin\"], \"label\": \"Test\"}" \
  https://gateway.watsonplatform.net/concept-expansion-beta/api/v1/upload

      

replacing username

and password

, you get something like:

{ "jobid": "R0xJTVBTRVJVTi4xMjA3MDEzMTM2MTk1OTk3OTgyM18xOzgyNjM4Mjg=" }

      



You do not need to specify Content-Type

or X-Synctimeout

.


Update Jun 11: I updated the answer with php curl code

<?php
header('Content-type: application/json');  

$services_json = json_decode(getenv('VCAP_SERVICES'), true);
$cred = $services_json["concept_expansion"][0]["credentials"];
// credentials
$username = $cred["username"];
$password = $cred["password"];
$url = $cred["url"] . '/v1/upload';

try {
    //List of terms to seed the concept expansion.
    $temp  = array('seeds' => array('motrin','aspirin','keflex' ) );  
    $data = array(
     'seeds' => $temp,
     'dataset' => 'mtsamples',
     'label' => 'drugs'   // label for the seeds
    );
    $data_string = json_encode($data);
    $curl = curl_init();

    curl_setopt($curl, CURLOPT_USERPWD, $username . $password);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: application/json'));

    $result = curl_exec($curl);
    curl_close($curl);
    echo $result;
} catch(Exception $e) {  
    echo $e->getMessage();
}  
?>  

      

+1


source







All Articles