3

我正在尝试在 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 = $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 错误。我有什么遗漏吗?

4

1 回答 1

1

我不知道 php,但看起来你正在尝试执行 curl 命令,所以我会给你 curl 命令:)

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

替换usernamepassword你会得到类似的东西:

{ "jobid": "R0xJTVBTRVJVTi4xMjA3MDEzMTM2MTk1OTk3OTgyM18xOzgyNjM4Mjg=" }

无需指定Content-TypeX-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();
}  
?>  
于 2015-06-11T23:53:46.420 回答