Linux 从 php 调用 C 程序并读取程序输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5555912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:32:02  来源:igfitidea点击:

Call a C program from php and read program output

phpclinux

提问by Niroshan

Could some one explain me how to run a C program from a php script and store console output of the C program to a php variable?

有人可以解释我如何从 php 脚本运行 C 程序并将 C 程序的控制台输出存储到 php 变量吗?

My program prints an integer value on the console using C printf() function. I want to read this value and store it in a php variable.
I am using linux. I tried exec but it doesn't display the variable value once echoed to the page

我的程序使用 C printf() 函数在控制台上打印一个整数值。我想读取这个值并将其存储在一个 php 变量中。
我正在使用 linux。我试过 exec 但它不显示一旦回显到页面的变量值

This the code snippet I am using.

这是我正在使用的代码片段。

exec("Release/matchface image1.jpg image2.jpg", $output);
while( list(,$row) = each($output) ) {
  echo $row. "<br />";
} 

采纳答案by Pascal MARTIN

You'll want to use the shell_exec()function (quoting):

您将要使用该shell_exec()功能(引用)

Execute command via shell and return the complete output as a string

通过 shell 执行命令并以字符串形式返回完整的输出

Which means something that will look like this :

这意味着看起来像这样:

$output = shell_exec('/path/to/your/program');


Or, you could use the backtick operator-- which would do exactly the same thing (quoting):


或者,您可以使用backtick operator-- 这将做完全相同的事情(引用)

PHP will attempt to execute the contents of the backticks as a shell command; the output will be returned

PHP 将尝试将反引号的内容作为 shell 命令执行;输出将被返回

And, in code :

而且,在代码中:

$output = `/path/to/your/program`;