从php调用C程序并读取程序输出 [英] Call a C program from php and read program output

查看:72
本文介绍了从php调用C程序并读取程序输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

我的程序使用C printf()函数在控制台上打印一个整数值.我想读取此值并将其存储在php变量中.
我正在使用linux.我尝试了exec,但是一旦回显到页面后就不会显示变量值

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

这是我正在使用的代码段.

This the code snippet I am using.

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

推荐答案

您将要使用 shell_exec() 函数(引用):

通过shell执行命令并返回 完整的输出作为字符串

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

这意味着以下内容:

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


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


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

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

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

而且,在代码中:

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

这篇关于从php调用C程序并读取程序输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆