Wednesday, December 10, 2014

Getting user modeling data from Watson


Getting user modeling data from Watson


[
Example of getting content and getting it processed by Watson user modeling interface.
It returns json result.
]

public function getwatsonreport1($sentcontent) {
        $randomString1 = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 8);

        $randomString2 = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 8);

        $data = array(

             "id" => $randomString1,

             "userid" => $randomString2,

             "sourceid" => "Ramesh-test",

             "contenttype" => "text/plain",

             "language" => "en",

             "content" => $sentcontent,

        );

        $contentItems1 = array("contentItems" => array($data));

        $data_string = json_encode($contentItems1);

        header('Content-type: application/json');

        $services_json = json_decode(getenv('VCAP_SERVICES'), true);

        $watson = $services_json["user_modeling"][0]["credentials"];

        $username = $watson["username"];

        $password = $watson["password"];

        $url = $watson["url"] . '/api/v2/profile';

        $auth = base64_encode($username . ":" . $password);

        try {

            $curl = curl_init();
            curl_setopt($curl, CURLOPT_HTTPHEADER, array(

                 'Content-Type: application/json',

                 'X-synctimeout: 30',

                 'Authorization: Basic ' . $auth,

                 'Content-Length: ' . strlen($data_string))

            );

 

            curl_setopt($curl, CURLOPT_POST, true);

            curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);

 

            curl_setopt($curl, CURLOPT_URL, $url);

            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

 

            $result = curl_exec($curl);

 

            curl_close($curl);

 

            return $result;

        } catch (Exception $e) {

            echo '<p>There Was an Error Accessing Watson !!!</p>';

            echo $e->getMessage();

        }

    }

 

 

No comments:

Post a Comment