集成IBM Watson Personality Insights API

时间:2017-04-03 05:05:00

标签: php curl libcurl ibm-watson personality-insights

我正在构建Web应用程序并希望集成IBM Watson Personality Insights API。 我正在使用PHP,并且必须使用Curl Library来实现相同的

以下是IBM文档中提到的使用Curl

的代码
curl -X POST --user {username}:{password}
--header "Content-Type: text/plain;charset=utf-8"
--header "Accept: application/json"
--data-binary @<filename>
"https://gateway.watsonplatform.net/personality-insights/api/v3/profile"

我如何在PHP中执行此操作?

我正在尝试这样做,但我得到一个空洞的回应

$ch2 = curl_init("https://gateway.watsonplatform.net/personality-insights/api/v3/profile");
$request_headers = array();
$request_headers[] = 'Content-Type: text/plain;charset=utf-8';
$request_headers[] = 'Content-Language: en';
$request_headers[] = 'Accept-Language: en';

$simple_data = 'Some dummy data';
    curl_setopt_array( $ch2, array(
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POSTFIELDS => $simple_data,
        CURLOPT_HTTPHEADER => $request_headers,
        CURLOPT_USERPWD => 'XXXX:YYYY',
    )
);
$response2 = curl_exec( $ch2 );

1 个答案:

答案 0 :(得分:0)

为了从API获得结果,拥有有效的SSL证书是一个很好的例子。 请尝试以下步骤

  1. 访问Firefox并访问https://gateway.watsonplatform.net/personality-insights/api/v3/profile
  2. 使用您的Watson PI凭据登录
  3. 点击网址栏最左侧的锁定,然后点击更多信息。
  4. 点击查看证书 - &gt;细节
  5. 层次结构中的第一个项目是IBM Watson PI的CA证书,将其导出到您的计算机。
  6. 然后在您的卷曲请求中使用此3行代码

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_CAINFO , getcwd() . "\Your Certificate Location");