New

Watson Personality Insights Service using PHP

Here is a sample reference of  using Watson Personality Insight service with PHP. Since this is not readily available as part of the BlueMix Watson examples, I am sharing this in my blog, The testservice.php is the sample test client which uses the WatsonService.php. The following code is for reference only. testservice.php …

getInsights($text); ?>

… WatsonService.php …

auth = $auth; $this->wsurl = $parsedUrl . "/api/v2/profile"; } /\*\* \* Create request for Watson PI service \*/ public function getInsights($datatext) { try { $datax = array( "contentItems" => array( $datarr = array( 'userid' => 'dummy', 'id' => 'someid', 'sourceid' => 'freetext', 'contenttype' => 'text/plain', 'language' => 'en', 'content' => $datatext))); $data\_string = json\_encode($datax); $curl = curl\_init(); //print "ok"; //print $this->auth; curl\_setopt($curl, CURLOPT\_HTTPHEADER, array( 'Content-Type: application/json', 'X-SyncTimeout: 60', 'Authorization: Basic ' . $this->auth, 'Content-Length: ' . strlen($data\_string)) ); curl\_setopt($curl, CURLOPT\_SSL\_VERIFYPEER, FALSE); curl\_setopt($curl, CURLOPT\_POST, true); curl\_setopt($curl, CURLOPT\_POSTFIELDS, $data\_string); curl\_setopt($curl, CURLOPT\_URL, $this->wsurl); curl\_setopt($curl, CURLOPT\_RETURNTRANSFER, true); $result = curl\_exec($curl); if(curl\_errno($curl)) { echo 'error:' . curl\_error($curl); } //print $result; curl\_close($curl); return $result; } catch(Exception $e) { echo '

There Was an Error Accessing Watson User Modelling Service!!!

'; echo $e->getMessage(); } } } ................................
ai