首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Bluemix PHP Watson个性洞察服务

Bluemix PHP Watson个性洞察服务
EN

Stack Overflow用户
提问于 2015-07-05 01:33:26
回答 2查看 554关注 0票数 0

下面代码中的错误是什么?它返回以下错误:

代码语言:javascript
复制
{ "error":"Internal server error (root cause: multipart\/form-data; boundary=----------------------------248f475465f9)",
  "code":404 }

代码:

代码语言:javascript
复制
<?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;
    }

?>
EN

回答 2

Stack Overflow用户

发布于 2015-07-05 03:53:39

您正在使用的url:

代码语言:javascript
复制
curl_setopt($curl, CURLOPT_URL, "https://gateway.watsonplatform.net/personality-insights/api");

应该是:

代码语言:javascript
复制
curl_setopt($curl, CURLOPT_URL, "https://gateway.watsonplatform.net/personality-insights/api/v2/profile");

我看到你用的是contentItems,你也可以把content-type: text/plaintext发送到正文中,不需要构建JSON对象。在正文中发送text的curl命令是:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2015-07-08 02:39:55

今天早上早些时候,我在dwAnswers上回答了这个问题。

您只需要在POST请求中使用"body“参数(如果不使用默认值,则需要"headers”)。正如API文档中所述,其他参数是HTTP HEADER请求的一部分。

以下是为使用Watson Personality Insights而修改的PHP代码:

代码语言:javascript
复制
     $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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31223397

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档