IBM Watson Natural Language Understanding只获得英语短语

时间:2017-06-21 18:11:09

标签: php curl post get watson

IBM Watson Natural Language Understanding的小版本在POST(在php中)收到时只能获得英文短语。 当我发布法语时,西班牙语或葡萄牙语不起作用。

我的代码:

include_once( 'JSON.php'); $ json = new Services_JSON();

//获取参数 $ protocol = utf8_decode(($ _ GET ['protocol'])?$ _GET ['protocol']:$ _POST ['protocol']); $ report = utf8_decode(($ _ GET ['report'])?$ _GET ['report']:$ _POST ['report']); $ class = utf8_decode(($ _ GET ['classified'])?$ _GET ['classified']:$ _POST ['classified']);

//致电Watson

    $report = strtoupper($report);         

    $username = 'xxxxxxx';
    $password = 'XXXXX';
    $url = 'https://gateway.watsonplatform.net/natural-language-understanding/api/v1/analyze?version=2017-02-27&text=Helloethics&features=entities,sentiment,keywords';

    // Set post arguments for call
    $post_args = array(
        'text' => $report
    );

  // Set header arguments for call
    $header_args = array(
        'Content-Type: text/plain',
        'Accept: application/json'
    );

  // Set options for REST call via curl
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);   
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_USERPWD, "xxxxxxxx:XXXXXXX");        
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header_args);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_args);

    $status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);

    // Actual REST call via curl and cleanup (closing) of curl call
    $result = curl_exec($curl);
    print_r($result);
    curl_close($curl);

只有当prashe是英语时,POST / GET才有效,但Watson会返回其他语言:

只有当prashe是英语时,POST才有效,但Watson会返回其他语言:

{“language”:“pt”,“entities”:[],“警告”:[“关键字:内部服务器错误”,“情绪:内部服务器错误”]}

但是,当我使用LOCAL VARIABLE时,OK,有效。请帮我!谢谢!

1 个答案:

答案 0 :(得分:0)

解决!

排队 $ report = utf8_decode(($ _ GET [' report'])?$ _GET [' report']:$ _POST [' report']);

删除utf8_decode。

作品!