I am trying to use Watson concept expansion service in php.
I am using the following code to upload the seed list-
<?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 is giving a 400 error.Is there anything i'm missing?
I don't kow php but looks like you are trying to execute a curl command so I will give you the 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 will get something like:
{ "jobid": "R0xJTVBTRVJVTi4xMjA3MDEzMTM2MTk1OTk3OTgyM18xOzgyNjM4Mjg=" }
No need to specify Content-Type
or X-Synctimeout
.
Update June 11: I've updated the answer with the 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();
}
?>