2017-02-21 86 views
0

我正在打电话给沃森的API,像这样:沃森返回415错误

$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\""} 

不知道为什么,这段文字是不是被正确发送以及我需要如何发送它。

+0

来自watson的错误是该帖子发送的头文件中不支持'application/x-www-form-urlencoded'。你确定一个职位应该工作吗? –

回答

2

您应该使用CURLOPT_HTTPHEADER而不是CURLOPT_HEADER来设置请求标头。

CURLOPT_HEADER是一个“开/关”开关,用于设置标题与请求与否。