下面代码中的错误是什么?它返回以下错误:
{ "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;
}
?>发布于 2015-07-05 03:53:39
您正在使用的url:
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"发布于 2015-07-08 02:39:55
今天早上早些时候,我在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);https://stackoverflow.com/questions/31223397
复制相似问题