接收变量输入从PHP到c ++ [英] Receive varying variable inputs from PHP into c++

查看:193
本文介绍了接收变量输入从PHP到c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序需要发送以下变量:



Bool int string vector< int> vector< string&



这是来自 PHP href =http://stackoverflow.com/questions/4279003/execute-c-program-with-php-script> 此处 此处



查看 此问题 关于(int argc,char ** argv []),我知道你可以将各种参数传递给一个需要运行的程序,但是这些都是以 字符序列



所以我的问题是:如果可能,你如何在 PHP 调用中发送跨多个变量的变量 C ++ 程序?



EDIT 函数调用看起来像?



是不是使用(int argc,char ** argv []) c>并执行:

  int main(bool var1,int var2,std :: vector< std :: string& var3,std :: string var4)
{
...
}


$ b b

因为这似乎使(int argc,char ** argv [])一个冗余?

解决方案

您可以使用 exec 在PHP中执行C ++程序:

  exec(./ your_program.exe $ parameters,$ output); 

这将传递 $ parameters 作为C ++程序的命令行参数。在 main(int argc,char ** argv)中,您可以通过 argc argv



为了一次传递多个变量,您应该使用某种序列化。这将把您的变量的内容转换为一个单一的字符串,然后您可以再次作为命令行参数传递。
你只需要确保C ++端知道如何将这个字符串反序列化为正确的变量/数据类型。



有很多可能性,



格式如JSON,XML, MessagePack 等等。您甚至可以使用 PHP自己的 serialize 方法来实现某些内容。 p>




为了使事情更清楚,让我们假设将此数据从PHP传递到C ++ :

  $ var1 = false; 
$ var2 = 123;
$ var3 = array(some,string,data);
$ var4 =anotherstring;

让我们进一步假设你有一个PHP函数 my_serialize()数组($ var1,$ var2,$ var3,$ var4)转换为虚构的序列化格式 false | 123 |< some ,string,data> | anotherstring

  $ serialized_data = my_serialize var2,$ var3,$ var4)); 
// $ serialized_data ==false | 123 |< some,string,data> |另一个字符串

//调用你的程序并传递数据
exec ./your_program.exe'$ serialized_data',$ output);

在程序中,序列化数据现在在 argv [1] / code>。
现在您需要一个函数 my_deserialize(),它将分析输入数据并将其转换为相应的数据格式。

  int main(int argc,char ** argv)
{
std :: cout< argv [1]<< std :: endl;
MyData data = my_deserialize(argv [1]);
std :: cout<< data.var4<< std :: endl; //输出另一个字符串
}

使用上面列出的序列化方法之一您不必关心 my_serialize my_deserialize 的实施细节。


I have a program that needs to be sent the following variables:

Bool int string vector<int> vector<string>

This will come from a PHP call as I've looked into here and here.

Looking at this question about (int argc, char** argv[]), I know that you can pass various arguments to a program that it needs so to run, but these are all in the form of a Character Sequence.

So my question is: If it is possible, how do you go about sending across multiple varying variables in a PHP call to a C++ program?

EDIT (Question continued) ...and what would the function call look like?

Is it as simple as not using the (int argc, char** argv[]) and just doing:

int main(bool var1, int var2, std::vector<std::string> var3, std::string var4)
{
...
}

Because that seems to make (int argc, char** argv[]) a tad redundant?

解决方案

You can execute your C++ program in PHP using exec:

exec("./your_program.exe $parameters", $output);

This will pass the contents of $parameters as a command line argument to your C++ program. Within main(int argc, char** argv), you can then access this data through argc and argv.

In order to pass multiple variables at once, you should use some kind of serialization. This will convert the contents of your variables into one single string which you can then pass again as a command line argument. You just have to make sure that the C++ side knows how to unserialize this string into the correct variables / datatypes again.

There is a multitude of possibilities, ranging from inventing your own format (don't do this) to using one of the established ones (do this).

Formats like JSON, XML, MessagePack, etc. come to mind. You could even implement something using PHP's own serialize method.


To make things more clear, let's assume that you want to pass this data from PHP to C++:

$var1 = false;
$var2 = 123;
$var3 = array("some","string","data");
$var4 = "anotherstring";

let's further assume you have a PHP function my_serialize() which converts array($var1, $var2, $var3, $var4) into the fictional serialization format false|123|<some,string,data>|anotherstring:

$serialized_data = my_serialize(array($var1, $var2, $var3, $var4));
// $serialized_data  == "false|123|<some,string,data>|anotherstring"

// invoke your program and pass the data
exec("./your_program.exe '$serialized_data'", $output);

Within your program, the serialized data is now within argv[1]. You would now need a function my_deserialize() which parses the input data and converts them into the respective data format.

int main(int argc, char** argv)
{
    std::cout << argv[1] << std::endl;
    MyData data = my_deserialize(argv[1]);
    std::cout << data.var4 << std::endl; // outputs "another string"
}

Use one of the serialization methods I listed above and you don't have to care about the implementational details of my_serialize and my_deserialize.

这篇关于接收变量输入从PHP到c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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