0
<?php
  $curl = curl_init();
     $post_args = array('body' => $data );
     $header_args = array(
         'Content-Type: text/plain',
         'Accept: application/json'
     );
     curl_setopt($curl, CURLOPT_POST, true);
     curl_setopt($curl, CURLOPT_POSTFIELDS, $post_args);
     curl_setopt($curl, CURLOPT_HTTPHEADER, $header_args);
     curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
     curl_setopt($curl, CURLOPT_USERPWD,"'xxx':'xxx'");
     curl_setopt($curl, CURLOPT_URL, "https://gateway.watsonplatform.net/personality-insights/api/v2/profile");
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

     $result = curl_exec($curl);

     curl_close($curl);

     $decoded = json_decode($result, true);
     ?>

I want to use IBM Bluemix Personality Insights with php curl and I am getting this error: Undefined variable: data What am I missing? How should I set up this variable, how should I pass the text that I want to have analyzed?

ralphearle
  • 1,696
  • 13
  • 18
Demotivated
  • 117
  • 3
  • 13

1 Answers1

1

In this php code what is $data variable you just assign to $post_args['body']. i think you are using in post filed so just try if you get somthing from url than

$post_args = array('body' => $_POST['data']) ; 

or just set some value if you sending to the url

$data = 'Your real data which you want to send in url  ' ; than use in array .
gaurav
  • 75
  • 1
  • 8
  • i looked over the internet and there are many files missing.why ibm hasnt provided with a php example of personality insights,is still unclear – Demotivated Sep 11 '16 at 16:32
  • I just tried your suggestion and there is no err now.The problem is that it is not displaying anything,in the html.how? – Demotivated Sep 11 '16 at 17:47
  • try to `var_dump($result)` – gaurav Sep 11 '16 at 18:09
  • It mean your `curl_exec()` function is failing [http://php.net/manual/en/function.curl-exec.php] – gaurav Sep 12 '16 at 13:50
  • the 4-th example that you sent me,that crated from a month ago,check this page:https://github.com/eciggaar/watsonpi-php/blob/master/watson_pi.php – Demotivated Sep 12 '16 at 13:56
  • I am stuck at://$watsonPi = $services_json[""][0]["credentials"]; ?? what is service name?// Curl initialization to make REST call to Watson service // Get textarea field from POST request // Set post arguments for call – Demotivated Sep 12 '16 at 13:57
  • you try this file which in cheatsheet (https://github.com/eciggaar/watsonpi-php/blob/master/cheatsheet/watsonpi-snippet.php) this explain more better than this file . Secondly your `index.html` file have form which have textarea tag they are sending this to `watson_pi.php` so now your data variable is `$data = $_POST['txtarea']` . My suggestion is you can try replace file in action attribute in `index.html` with `watsonpi-snippet.php` try this file – gaurav Sep 12 '16 at 14:12
  • the raw text starts from $data....I remove the $data = $_POST["txtarea"]; and replace it with $data='some text'; still it echos raw php starting from $data and below – Demotivated Sep 12 '16 at 15:03
  • What is it showin when you `var_dump($result)` now – gaurav Sep 12 '16 at 18:55
  • i havent inserted it since it has an echo itself,but its surprising why the code doesnt run full,up to $data then all php is printed like text – Demotivated Sep 12 '16 at 20:46
  • i finay got it running,you need to change the index.html to .php and it echos null now. – Demotivated Sep 13 '16 at 07:40
  • there is no code of php in `index.html` so why we have to change but it works good for you – gaurav Sep 13 '16 at 07:43
  • the file runs now but it echos null and I dont know why?I even try to set the $data='some string'; and it still echos null.Seriosly I dont understand where the heck the problem is... – Demotivated Sep 13 '16 at 08:05