gpt4 book ai didi

php - 将 PHP 中的 $_POST 变量传递给由 exec() 函数运行的 C++ 程序

转载 作者:行者123 更新时间:2023-11-30 05:15:49 25 4
gpt4 key购买 nike

我有以下用于测试目的的 C++ 程序:

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>

using namespace std;

int main()
{
int len;
char* lenstr = getenv("CONTENT_LENGTH");

if (lenstr != NULL && (len = atoi(lenstr)) != 0)
{
char* post_data = new char[len];
fgets(post_data, len + 1, stdin);
cout << post_data << endl;
delete post_data;
}

这应该简单地获取一个环境变量并将其显示在屏幕上。我要从中调用此函数的 PHP 文件如下(我通过 AJAX 调用):

<?php
$sent = $_POST['source'];
if ($sent != "")
{
echo exec("test.exe");
}
else
echo "Please enter text";
?>

现在的问题是,如何将 $sent 作为要在其中处理的参数输入到 C++ 程序中?我已经在 Internet 上和 SO 上做了一些研究,但到目前为止我一直失败。

最佳答案

要在使用 exec 函数时从 php 传递参数,您可以这样做:

exec("test.exe '" . $_POST['xParameter'] . "'");

然后您将被允许使用传统方式从您的 C++ 端接收它以接收终端参数,如下所示:

int main(int argc, char *argv[])

更新

这是关于如何使用 C 语法从 php 传递参数的工作版本 not C++

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
printf("hello %s\n", argv[1]);
}

然后,在 php 中:

echo shell_exec('./hello Hassan');

// Output: hello Hassan

如果你使用了exec:

exec('./hello Hassan', $out);
print_r($out);
// Output: Array ([0] => hello Hassan)

关于php - 将 PHP 中的 $_POST 变量传递给由 exec() 函数运行的 C++ 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42931702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com