Php 沃森概念拓展服务
我正在尝试在php中使用Watson概念扩展服务。Php 沃森概念拓展服务,php,curl,ibm-watson,Php,Curl,Ibm Watson,我正在尝试在php中使用Watson概念扩展服务。 我正在使用以下代码上载种子列表- <?php header('Content-type: application/json'); $services_json = json_decode(getenv('VCAP_SERVICES'), true); $cred = $services_json["concept_expansion"][0]["credentials"]; // credentials $username
我正在使用以下代码上载种子列表-
<?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();
}
?>
但是代码给出了一个400错误。我缺少什么吗?我不懂php,但看起来您正在尝试执行curl命令,所以我将给您curl命令:) 替换
用户名
和密码
您将得到如下结果:
{ "jobid": "R0xJTVBTRVJVTi4xMjA3MDEzMTM2MTk1OTk3OTgyM18xOzgyNjM4Mjg=" }
无需指定内容类型
或X-Synctimeout
6月11日更新:我已经用php curl代码更新了答案
<?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();
}
?>
OP未使用命令行curl
程序。
<?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();
}
?>