首页 > 解决方案 > 使用 php CURL 将文档添加到 IBM Watson Discovery

问题描述

我一直在尝试使用使用 PHP CURL 的 Watson Discovery API 将文档添加到集合中,但没有成功。这是我到目前为止所得到的:

    $doc = 'documents/comment_' . $id . '.json';
    $fields = [
        'file' => new \CURLFile($doc, 'application/json', 'comment_' . $id)
    ];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url . $method);

    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_USERPWD, 'apikey:' . $apikey);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);

    curl_close($ch);
    return $result;

我意识到这可能不正确,但我尝试了各种组合,但没有得到任何结果——即没有错误消息,什么也没有。任何人都可以建议我可能会出错的地方吗?

PHP 7.0 (Laravel)

标签: phpibm-watson

解决方案


尝试类似的方法,至少获得一些“为什么”的信息:

$response = curl_exec($ch);
if($response == false){
    $curl_errno = curl_errno($ch);
    $curl_error = curl_error($ch);
}
$curl_info = curl_getinfo($ch);

此外,在那里安装xDebug可能会有很大帮助。


推荐阅读