C++ popen 运行 PHP

发布于 2024-12-11 21:01:26 字数 834 浏览 0 评论 0原文

我正在使用 http://www.jukie.net/bart/blog/popenRWE 并在使用 cat 命令时编写下面的脚本

int pipes[3];
int pid;
const char *const args[] = {
    "php ",
    NULL
};
pid = popenRWE(pipes, args[0], args); 

char *cmd = "<?php echo 'hello world';?> ";
cout << "write: " << write(pipes[0], cmd, strlen(cmd)) << endl;
cout << "err: " << errno << endl;

char res[100];
cout << "read: " << read(pipes[1], res, 100) << endl;
cout << "result: " << res << endl;

,它可以工作,输入是输出(这就是 cat 所做的),但使用 php 读取的是空的。 确认 php 已安装并位于我的路径上

我已通过运行echo "" | php

直接在控制台运行 ,并得到输出。有人可以就这段代码提供建议或帮助吗?提前致谢。

i'm using popenRWE from http://www.jukie.net/bart/blog/popenRWE and making the script below

int pipes[3];
int pid;
const char *const args[] = {
    "php ",
    NULL
};
pid = popenRWE(pipes, args[0], args); 

char *cmd = "<?php echo 'hello world';?> ";
cout << "write: " << write(pipes[0], cmd, strlen(cmd)) << endl;
cout << "err: " << errno << endl;

char res[100];
cout << "read: " << read(pipes[1], res, 100) << endl;
cout << "result: " << res << endl;

when i use cat command, it works, the input is the ouput (that's what cat doing), but using php the read is empty. i have confirmed that php is installed and on my path by running

echo "<?php echo 'hello world';?>" | php

directly on the console, and got the output. Can someone please advise or help on this code? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灯下孤影 2024-12-18 21:01:26

您的代码存在三个问题:

  • 没有名为 "php " 的可执行文件。只有 "php" (请注意,没有空格)。这不起作用的原因是因为 popenRWE 使用 execvp 它不会启动 shell 来执行命令,但它需要您要执行的二进制文件的文件名(但它会在 $PATH 中搜索它)。
  • 写入数据后,您应该关闭 stdin 文件句柄,否则您可能需要无限期地等待输出写入。
  • 另外,您应该使用 waitpid 等待 php 进程完成,否则您可能会“丢失”一些输出。

总结一下:

int pipes[3];
int pid;
const char *const args[] = {
    "php",
    NULL
};
pid = popenRWE(pipes, args[0], args);

char *cmd = "<?php echo 'hello world', \"\\n\";?> ";
cout << "write: " << write(pipes[0], cmd, strlen(cmd)) << endl;
cout << "err: " << errno << endl;
close(pipes[0]);

// TODO: proper error handling
int status;
waitpid(pid, &status, 0);

char res[100];
int bytesRead = read(pipes[1], res, (sizeof(res)/sizeof(char))-1);
// zero terminate the string
res[bytesRead >= 0 ? bytesRead : 0] = '\0';

cout << "read: " << bytesRead << endl;
cout << "result: " << res << endl;

There are three problems with your code:

  • There is no executable named "php ". There is just "php" (notice that there is no space). The reason why this does not work is beceause popenRWE uses execvp which does not start a shell to execute the command but it expects the filename of the binary you want to executed (it searches for it in $PATH though).
  • You should close the stdin-filehandle after you've written your data, otherwise you might have to wait indefinitely for the output to be written.
  • Also you should wait for the php-process to finish using waitpid because otherwise you might "lose" some of the output.

To wrap it up:

int pipes[3];
int pid;
const char *const args[] = {
    "php",
    NULL
};
pid = popenRWE(pipes, args[0], args);

char *cmd = "<?php echo 'hello world', \"\\n\";?> ";
cout << "write: " << write(pipes[0], cmd, strlen(cmd)) << endl;
cout << "err: " << errno << endl;
close(pipes[0]);

// TODO: proper error handling
int status;
waitpid(pid, &status, 0);

char res[100];
int bytesRead = read(pipes[1], res, (sizeof(res)/sizeof(char))-1);
// zero terminate the string
res[bytesRead >= 0 ? bytesRead : 0] = '\0';

cout << "read: " << bytesRead << endl;
cout << "result: " << res << endl;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文