Watson 返回 415 错误

Watson returning 415 error

我正在打电话给 Watson 的 API,就像这样:

$url2 = 'https://watson-api-explorer.mybluemix.net/personality-insights/api/v3/profile?raw_scores=false&csv_headers=false&consumption_preferences=true&version=2017-02-01';
$request_headers = array();
$request_headers[] = 'Accept: application/json';
$request_headers[] = 'Content-Type: text/plain';

$request_headers[] = 'Content-Language: en';
$request_headers[] = 'Accept-Language: en';

$simple_data = 'some really interesting text goes here';


$plain_data = json_encode($data);
$ch2 = curl_init( $url2 );
curl_setopt_array( $ch2, array(
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => $simple_data,
    CURLOPT_FOLLOWLOCATION => 1,
    CURLOPT_HEADER => $request_headers,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_USERPWD => 'XXX:YYY' //removed for illustration
)
);
$response2 = curl_exec( $ch2 );
var_dump($response2);

这样一直报错如下:

{"code":415,"sub_code":"S00002","error":"Unsupported Content-Type: \"application\/x-www-form-urlencoded\""}

不确定为什么没有正确发送此文本以及我需要如何发送它。

您应该使用 CURLOPT_HTTPHEADER 而不是 CURLOPT_HEADER,以设置请求 headers。

CURLOPT_HEADER 是一个 "on/off" 开关,用于 headers 是否随请求设置。

https://watson-api-explorer.mybluemix.net is an anonymous proxy for testing purposes with rate and usage limits -- programmatic access should go through https://gateway.watsonplatform.net 使用从 Bluemix.net.

获得的凭据