以下代码中的错误是什么?它返回此错误:
{ "error":"Internal server error (root cause: multipart\/form-data; boundary=----------------------------248f475465f9)",
"code":404 }
代码:
<?php
function testLangID($data) {
$curl = curl_init();
$headers_arr = array(
"contentItems" => array(
"userid" => "dummyuserid",
"id" => "dummyid",
"sourceid" => "freetext",
"contenttype" => "application/json",
"language" => "en",
"content" => $data
)
);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $headers_arr);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "........:........");
curl_setopt($curl, CURLOPT_URL, "https://gateway.watsonplatform.net/personality-insights/api");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
curl_close($curl);
$decoded = json_decode($result, true);
return $result;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$res= testLangID( $_POST["textLID"] );
echo $res;
}
?>
答案 0 :(得分:1)
您正在使用的网址:
curl_setopt($curl, CURLOPT_URL, "https://gateway.watsonplatform.net/personality-insights/api");
应该是:
curl_setopt($curl, CURLOPT_URL, "https://gateway.watsonplatform.net/personality-insights/api/v2/profile");
我看到您正在使用contentItems
,您还可以发送content-type: text/plain
并且正文中的text
无需构建JSON对象。
在正文中发送text
的curl命令是:
curl -X POST -u USERNAME:PASSWORD \
-H "Content-Type: text/plain" \
-d "Text to analyze" \
"https://gateway.watsonplatform.net/personality-insights/api/v2/profile"
答案 1 :(得分:1)
今天早上我在dwAnswers回答了这个问题。
您只需要POST请求中的“body”参数(如果不使用默认值,则为“headers”)。如API文档中所述,其他参数是HTTP HEADER请求的一部分。
以下是经过修改以与Watson Personality Insights配合使用的PHP代码:
$curl = curl_init();
$post_args = array(
'body' => $data
);
$header_args = array(
'Content-Type: text/plain',
'Accept: application/json'
);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_args);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header_args);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD,<user id>:<password>");
curl_setopt($curl, CURLOPT_URL, "https://gateway.watsonplatform.net/personality-insights/api/v2/profile");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
curl_close($curl);
$decoded = json_decode($result, true);