octave_php_wrapper可以通过Octave软件源安装。
>> pkg install -forge octave_php_wrapper
octave_php_wrapper可以在线安装。
>> pkg install 'https://github.com/CNOCTAVE/octave_php_wrapper/releases/download/1.0.0/octave_php_wrapper.tar.gz'
octave_php_wrapper可以直接使用tar包安装。
>> pkg install octave_php_wrapper.tar.gz -local
>> pkg load octave_php_wrapper
$ sudo dnf install php octave-cli
Loadable Function: php_wrapper_string = create_php_wrapper (FILENAME, FILE_PATH='',
FORCE_OVERWRITE=false)
输入Octave脚本名FILENAME,生成可以运行Octave脚本的PHP文件。
返回PHP代码字符串。
可选参数FILE_PATH,如果不为空字符串,则用chdir()函数切换到对应目录之下。
可选参数FORCE_OVERWRITE,如果为false,则在PHP文件已经存在的情况下报错;
如果为true,则在PHP文件已经存在的情况下强制写入。
Loadable Function: php_wrapper_string = gen_php_wrapper_string (FILENAME)
输入Octave脚本名FILENAME,返回PHP代码字符串。
Loadable Function: octave_php_wrapper_example_string = octave_php_wrapper_examples (NAME)
获取octave_php_wrapper的用例。
支持的用例如下:
pass_args_get | GET方法传参
pass_args_post | POST方法传参
pass_args_put | PUT方法传参
pass_args_patch | PATCH方法传参
pass_args_delete | DELETE方法传参
jsondecode | 将JSON字符串解析为Octave数据
jsonencode | 将Octave数据序列化为JSON字符串
base64_decode | 将Base64字符串解析为Octave数据
base64_encode | 将Octave数据序列化为Base64字符串
upload_file | PHP上传文件
download_file | PHP下载文件
sum = 0;
for (i=linspace(1, 10))
sum += i;
endfor
disp(sum);
>> create_php_wrapper("add1to10.m")
ans = <?php
ob_start();
system('octave-cli add1to10.m');
$output = ob_get_clean();
echo $output;
?>
<?php
ob_start();
system('octave-cli add1to10.m');
$output = ob_get_clean();
echo $output;
?>
$ php add1to10.m.php
55
http://cnoctave.cn/add1to10.m.php
args = argv();
n = args(1);
sum = 0;
for (i=linspace(1, str2num(n)))
sum += i;
endfor
disp(sum);
<?php
if (isset($_GET['n'])) {
$n = $_GET['n'];
ob_start();
system('octave-cli add1to10.m' . ' ' . escapeshellarg($n));
$output = ob_get_clean();
echo $output;
} else {
echo "Param n should not be null.";
}
?>
>> octave_php_wrapper_examples("pass_args_get")
<?php
if (isset($_GET['n'])) {
$n = $_GET['n'];
echo escapeshellarg($n);
} else {
echo "Param n should not be null.";
}
?>
>> octave_php_wrapper_examples("pass_args_post")
<?php
if (isset($_POST['n'])) {
$n = $_POST['n'];
echo escapeshellarg($n);
} else {
echo "Param n should not be null.";
}
?>
>> octave_php_wrapper_examples("pass_args_put")
<?php
if ($_SERVER['REQUEST_METHOD'] === 'PUT') {
$n = file_get_contents('php://input');
echo escapeshellarg($n);
} else {
echo "This script only handles PUT requests.";
}
?>
>> octave_php_wrapper_examples("pass_args_patch")
<?php
if ($_SERVER['REQUEST_METHOD'] === 'PATCH') {
$n = file_get_contents('php://input');
echo escapeshellarg($n);
} else {
echo "This script only handles PATCH requests.";
}
?>
>> octave_php_wrapper_examples("pass_args_delete")
<?php
if ($_SERVER['REQUEST_METHOD'] === 'DELETE') {
parse_str($_SERVER['QUERY_STRING'], $queryParams);
if (isset($queryParams['n'])) {
$n = $queryParams['n'];
echo escapeshellarg($n);
} else {
echo "Param n should not be null.";
}
} else {
echo "This script only handles DELETE requests.";
}
?>
https://docs.octave.org/latest/JSON-data-encoding_002fdecoding.html
>> octave_php_wrapper_examples("jsondecode")
args = argv();
n = jsondecode(args(1));
disp(sum(n));
其中,[1,2,3,4,5,6,7]对应的JSON字符串是"[1,2,3,4,5,6,7]"。
https://docs.octave.org/latest/JSON-data-encoding_002fdecoding.html
>> octave_php_wrapper_examples("jsonencode")
n = [1,2,3,4,5,6,7];
disp(jsonencode(n));
其中,[1,2,3,4,5,6,7]对应的JSON字符串是"[1,2,3,4,5,6,7]"。
https://docs.octave.org/latest/Base64-and-Binary-Data-Transmission.html
>> octave_php_wrapper_examples("base64_decode")
args = argv();
n = base64_decode(args(1));
disp(sum(n));
其中,[1,2,3,4,5,6,7]对应的Base64字符串是"AAAAAAAA8D8AAAAAAAAAQAAAAAAAAAhAAAAAAAAAEEAAAAAAAAAUQAAAAAAAABhAAAAAAAAAHEA="。
https://docs.octave.org/latest/Base64-and-Binary-Data-Transmission.html
>> octave_php_wrapper_examples("base64_encode")
n = [1,2,3,4,5,6,7];
disp(base64_encode(n));
其中,[1,2,3,4,5,6,7]对应的Base64字符串是"AAAAAAAA8D8AAAAAAAAAQAAAAAAAAAhAAAAAAAAAEEAAAAAAAAAUQAAAAAAAABhAAAAAAAAAHEA="。
>> octave_php_wrapper_examples("upload_file")
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['fileToUpload']) && $_FILES['fileToUpload']['error'] == UPLOAD_ERR_OK) {
$fileTmpPath = $_FILES['fileToUpload']['tmp_name'];
$fileName = $_FILES['fileToUpload']['name'];
$fileSize = $_FILES['fileToUpload']['size'];
$fileType = $_FILES['fileToUpload']['type'];
$fileNameCmps = explode(".", $fileName);
$fileExtension = strtolower(end($fileNameCmps));
$allowedfileExtensions = array('jpg', 'jpeg', 'png', 'gif');
if (in_array($fileExtension, $allowedfileExtensions)) {
$uploadFileDir = 'uploads/';
if (!is_dir($uploadFileDir)) {
mkdir($uploadFileDir, 0777, true);
}
$dest_path = $uploadFileDir . $fileName;
if(move_uploaded_file($fileTmpPath, $dest_path)) {
echo "File is valid and was successfully uploaded.\n";
} else {
echo "There was some error moving the file to upload directory. Please make sure the upload directory is writable.";
}
} else {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}
} else {
echo "No file was uploaded. Sorry, your file was not uploaded.";
}
?>
其中,$dest_path是PHP上传文件并保存的路径。Octave脚本获取这个参数即可继续处理文件。
>> octave_php_wrapper_examples("download_file")
<?php
$filePath = 'path/to/your/file.zip';
if (!file_exists($filePath)) {
die('File not found');
}
$fileSize = filesize($filePath);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($filePath) . '"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . $fileSize);
ob_clean();
flush();
readfile($filePath);
exit;
?>
其中,$filePath是PHP下载文件的路径。
@misc{CNOCTAVE2025, author = {Yu Hongbo, }, title = {octave_php_wrapper}, year = {2025}, howpublished = {\url{https://github.com/CNOCTAVE/octave_php_wrapper}}, }
@techreport{CNOCTAVE2025, author = {Yu Hongbo, }, title = {octave_php_wrapper Document}, institution = {BA DU XIN SHANG}, year = {2025}, number = {16}, month = {5}, url = {https://cnoctave.github.io/octave_php_wrapper/index.html}, urldate = {2025-05-16}, }
@article{https://engrxiv.org/preprint/view/4639, doi = {10.31224/4639}, url = {https://engrxiv.org/preprint/view/4639}, author = {Yu Hongbo, }, keywords = {Octave, GNU, PHP, Wrapper, Numerical Computations, Web Applications}, title = {octave_php_wrapper: Bridge the Gap Between GNU Octave and Modern Internet Service}, publisher = {engrxiv}, year = {2025}, copyright = {Creative Commons Attribution 4.0 International} }