如何通过openCPU文件传递按预期在PHP中进行CURL

时间:2019-03-30 14:30:16

标签: php curl datatable php-curl opencpu

我正在尝试使用ajax在HTML上生成数据表,该表调用php以获得JSON。

HTML

<div id="first">
    <button type="button" id="fetch_with_scores" hidden = true >Calculate Scores</button>
</div>
<div id="mytable_with_scores_div" >
<table id="mytable_with_scores" class="display nowrap" style="width:100%" >
    <thead>
    <tr>
        <th>Id</th>
        <th>EmpName</th>
        <th>EmpSal</th>
        <th>EmpAge</th>
        <th>HasFilledSurvey</th>

    </tr>
    </thead>
</table>
</div>

JAVASCRIPT

$(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'
        });
    });
});
  • 单击按钮时,数据表jquery必须调用send_recv_json.php,其中必须将JSON文件作为输入发送到openCPU URL,并从openCPU R代码生成JSON输出,我需要使用该代码来填充数据表。 / li>
  • 使用输入文件来CURL openCPU URL:

curl -L http://192.168.1.204/ocpu/library/myPackage/R/calculate_scores -F "x=@5c9e2d185e925.json"

并获得结果:

curl -L http://192.168.1.204//ocpu/tmp/x01175df6a9b656/R/.val

其中x01175df6a9b656是第一次卷曲后的session_id。

  • 在php中,我正在使用exec_shell()来按以下方式运行这些curl:
// 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" => 1,
    "iTotalRecords" => count($output_data_arr),
    "iTotalDisplayRecords" => count($output_data_arr),
    "aaData" => $output_data_arr ];

echo json_encode($results);
  • 从外壳运行php时,我得到了预期的输出JSON。 输出非常适合数据表:

{"sEcho":1,"iTotalRecords":3,"iTotalDisplayRecords":3,"aaData":[["1","abc","200","34","Y"],["2","def","400","44","Y"],["3","ghi","600","35","N"]]}

但是当我从apache运行时,数据表ajax javascript抛出错误:

    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.<anonymous> (jquery-3.3.1.js:9548)

表示未从php填充数据。

我对php不太熟悉。 curl_init', 'curl_setoptcurl_exec有帮助吗?如果是,如何设计这些以满足上面的openCPU curl期望。即如何专门运行:

curl -L http://192.168.1.204/ocpu/library/myPackage/R/calculate_scores -F "x=@5c9e2d185e925.json"

原样需要"x=@<file_name.json"作为参数。

任何指针都将非常有帮助。

1 个答案:

答案 0 :(得分:0)

经过一番环顾后,我想现在使用更高版本(例如7和所有版本)的php,以便进行多部分POST卷曲来发送文件,我们需要创建一个 CurlFile个对象,具有文件名和mimetype。 https://www.php.net/manual/en/class.curlfile.php

就我而言,我尝试的卷曲是: curl -L http://192.168.1.204/ocpu/library/myPackage/R/calculate_scores -F "x=@5c9e2d185e925.json"

按以下方式运行后,我可以成功获取响应并将响应用于数据表,如下所示:

PHP


curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
$args['x'] = new CurlFile($file_name, 'application/json');
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // This will make curl_exec return the data instead of outputting it.

$output = curl_exec($ch);

//print_r($output);

if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

// 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
*/


$score_result_loc = strtok($output, "\n");
$score_url = $r_server . $score_result_loc . '/print';
unlink($file_name);

//echo $score_url;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $score_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$output_arr = json_decode(curl_exec($ch));
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);


$results = ["sEcho" => 1,
    "iTotalRecords" => count($output_arr),
    "iTotalDisplayRecords" => count($output_arr),
    "aaData" => $output_arr ];

//print_r($results);


echo json_encode($results);

/* The output of the above echo:
{"sEcho":1,"iTotalRecords":3,"iTotalDisplayRecords":3,"aaData":[["1","abc","200","34","Y"],["2","def","400","44","Y"],["3","ghi","600","35","N"]]}
*/

JAVASCRIPT

$(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'
        });
    });
});