title: How to CURL in PHP as expected by openCPU file passing
body: <p>I am trying to generate a datatable on my HTML using ajax which calls a php to obtain JSON. </p>
  
  <p><strong>HTML</strong></p>
  
  <pre><code>&lt;div id="first"&gt;
      &lt;button type="button" id="fetch_with_scores" hidden = true &gt;Calculate Scores&lt;/button&gt;
  &lt;/div&gt;
  &lt;div id="mytable_with_scores_div" &gt;
  &lt;table id="mytable_with_scores" class="display nowrap" style="width:100%" &gt;
      &lt;thead&gt;
      &lt;tr&gt;
          &lt;th&gt;Id&lt;/th&gt;
          &lt;th&gt;EmpName&lt;/th&gt;
          &lt;th&gt;EmpSal&lt;/th&gt;
          &lt;th&gt;EmpAge&lt;/th&gt;
          &lt;th&gt;HasFilledSurvey&lt;/th&gt;
  
      &lt;/tr&gt;
      &lt;/thead&gt;
  &lt;/table&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>JAVASCRIPT</strong></p>
  
  <pre><code>$(document).ready(function() {
     $('#fetch_with_scores').on('click', function(e) {
      $('#mytable_with_scores').DataTable( {
              "bProcessing": true,
              "bServerSide": true,
              "sAjaxSource": "send_recv_json.php",
              dom: 'Bfrtip'
          });
      });
  });
  </code></pre>
  
  <ul>
  <li>On click of a button datatable jquery must call <code>send_recv_json.php</code> where a a JSON file must be sent to openCPU URL as input and a JSON output is generated from openCPU R code which I need to use to populate the datatable.</li>
  <li>to CURL openCPU URL with an input file:</li>
  </ul>
  
  <p><code>curl -L http://192.168.1.204/ocpu/library/myPackage/R/calculate_scores -F "x=@5c9e2d185e925.json"</code></p>
  
  <p>and to obtain the result:</p>
  
  <p><code>curl -L http://192.168.1.204//ocpu/tmp/x01175df6a9b656/R/.val</code></p>
  
  <p>where <code>x01175df6a9b656</code> is the session_id after first curl.</p>
  
  <ul>
  <li>In php I am using <code>exec_shell()</code> to run these curls as below:</li>
  </ul>
  
  <pre><code>// first curl
  $r_server = 'http://192.168.1.204';
  $url =  $r_server . '/ocpu/library/myPackage/R/calculate_scores/';
  $shell_cmd_str = "curl -L " . $url . " -F " . "\"x=@$file_name\"";
  
  $output = shell_exec($shell_cmd_str);
  unlink($file_name);
  
  // First curl output:
  /*
  /ocpu/tmp/x044a9afe02d4bd/R/.val
  /ocpu/tmp/x044a9afe02d4bd/R/calculate_scores
  /ocpu/tmp/x044a9afe02d4bd/stdout
  /ocpu/tmp/x044a9afe02d4bd/source
  /ocpu/tmp/x044a9afe02d4bd/console
  /ocpu/tmp/x044a9afe02d4bd/info
  /ocpu/tmp/x044a9afe02d4bd/files/5c9f5ee722ef7.json
  /ocpu/tmp/x044a9afe02d4bd/files/DESCRIPTION
  */
  
  // second curl
  $score_result_loc = strtok($output, "\n"); // first line has the output from above
  $score_url = $r_server . $score_result_loc;
  $shell_cmd_str_for_scores = "curl -L " . $score_url ;
  
  $output_data = shell_exec($shell_cmd_str_for_scores);
  $output_data_arr = json_decode($output_data);
  
  
  $results = ["sEcho" =&gt; 1,
      "iTotalRecords" =&gt; count($output_data_arr),
      "iTotalDisplayRecords" =&gt; count($output_data_arr),
      "aaData" =&gt; $output_data_arr ];
  
  echo json_encode($results);
  </code></pre>
  
  <ul>
  <li>I get the expected output JSON when I run the php from shell.
  The output is perfect for the datatable:</li>
  </ul>
  
  <p><code>{"sEcho":1,"iTotalRecords":3,"iTotalDisplayRecords":3,"aaData":[["1","abc","200","34","Y"],["2","def","400","44","Y"],["3","ghi","600","35","N"]]}</code></p>
  
  <p>But when I run from apache somehow datatable ajax javascript is throwing error: </p>
  
  <pre><code>    at wb (jquery.dataTables.min.js:40)
      at jquery.dataTables.min.js:37
      at i (jquery.dataTables.min.js:35)
      at Object.success (jquery.dataTables.min.js:36)
      at fire (jquery-3.3.1.js:3268)
      at Object.fireWith [as resolveWith] (jquery-3.3.1.js:3398)
      at done (jquery-3.3.1.js:9305)
      at XMLHttpRequest.&lt;anonymous&gt; (jquery-3.3.1.js:9548)
  </code></pre>
  
  <p>which indicates data is not populated from php.</p>
  
  <p>I am not very familiar with php. Would <code>curl_init', 'curl_setopt</code>, <code>curl_exec</code> help ? If yes, how to design these to meet the above openCPU curl expectations. i.e. how to run specifically :</p>
  
  <p><code>curl -L http://192.168.1.204/ocpu/library/myPackage/R/calculate_scores -F "x=@5c9e2d185e925.json"</code> </p>
  
  <p>where <code>"x=@&lt;file_name.json"</code> is needed as parameter as it is.</p>
  
  <p>Any pointers would be very helpful.</p>