[英]How to CURL in PHP as expected by openCPU file passing
I am trying to generate a datatable on my HTML using ajax which calls a php to obtain JSON. 我试图使用ajax在我的HTML上生成一个数据表,调用php来获取JSON。
HTML 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 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'
});
});
});
send_recv_json.php
where aa 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. 单击一个按钮数据表jquery必须调用send_recv_json.php
,其中必须将一个JSON文件作为输入发送到openCPU URL,并从openCPU R代码生成JSON输出,我需要使用它来填充数据表。 curl -L http://192.168.1.204/ocpu/library/myPackage/R/calculate_scores -F "x=@5c9e2d185e925.json"
and to obtain the result: 并获得结果:
curl -L http://192.168.1.204//ocpu/tmp/x01175df6a9b656/R/.val
where x01175df6a9b656
is the session_id after first curl. 其中x01175df6a9b656
是第一次卷曲后的session_id。
exec_shell()
to run these curls as below: 在php中我使用exec_shell()
运行这些卷发如下: // 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);
{"sEcho":1,"iTotalRecords":3,"iTotalDisplayRecords":3,"aaData":[["1","abc","200","34","Y"],["2","def","400","44","Y"],["3","ghi","600","35","N"]]}
But when I run from apache somehow datatable ajax javascript is throwing error: 但是当我从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)
which indicates data is not populated from php. 表示数据未从php填充。
I am not very familiar with php. 我对php不太熟悉。 Would curl_init', 'curl_setopt
, curl_exec
help ? curl_init', 'curl_setopt
, curl_exec
帮助吗? If yes, how to design these to meet the above openCPU curl expectations. 如果是的话,如何设计这些以满足上述openCPU卷曲的期望。 ie how to run specifically : 即如何具体运行:
curl -L http://192.168.1.204/ocpu/library/myPackage/R/calculate_scores -F "x=@5c9e2d185e925.json"
where "x=@<file_name.json"
is needed as parameter as it is. 其中"x=@<file_name.json"
需要作为参数。
Any pointers would be very helpful. 任何指针都会非常有用。
after looking around a bit, I figured in php now with higher versions (like 7 and all) in order to do multipart POST curl to send a file, we need to create a CurlFile
object with file name and mimetype. 看了一下之后,我想在php中使用更高版本(比如7和all)为了做多部分POST curl来发送文件,我们需要创建一个带文件名和mimetype的CurlFile
对象。 https://www.php.net/manual/en/class.curlfile.php https://www.php.net/manual/en/class.curlfile.php
In my case the curl I was attempting was: 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/library/myPackage/R/calculate_scores -F "x=@5c9e2d185e925.json"
After running it as following, I was successfully able to get the response back and use the response for datatable as below: 按照以下方式运行后,我成功地获得了响应并使用数据表的响应,如下所示:
PHP 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 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'
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.