2

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?

Community
  • 1
  • 1
MLMLTL
  • 1,519
  • 5
  • 21
  • 35
  • 1
    You can make PHP execute the C++ program as a new process, see [here](http://stackoverflow.com/a/6796640). If you want to pass complex arguments, you might want to serialise the arguments in some way. A more complex approach would be to compile the C++ program as a library and load it using something like [`dl`](http://php.net/manual/en/function.dl.php), although I'm just guessing, as I have no experience with that. – Spooky Jun 29 '15 at 11:39
  • `exec()` did look the way to go, but using `exec()` to pass commands, serialised or not, would I set up the function with `(int argc, char** argv[])` or `(bool var1, int var2, std::vector var3, std::string var4)`? – MLMLTL Jun 29 '15 at 11:47
  • 1
    you **always** have to use `(int argc, char** argv[])` – m.s. Jun 29 '15 at 12:05
  • @m.s. But how can you send across integers, bools, vectors etc to a Character Sequence? – MLMLTL Jun 29 '15 at 12:09
  • 1
    I already wrote that in my answer below: You must **serialize** your data into character sequence. – m.s. Jun 29 '15 at 12:14
  • Ohhh, thanks for the clarification. But in my c++ code, I can't assign `char**` to `std::vector` – MLMLTL Jun 29 '15 at 12:16
  • 1
    you don't assign it like this. I updated my answer – m.s. Jun 29 '15 at 12:39

1 Answers1

4

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.

Community
  • 1
  • 1
m.s.
  • 16,063
  • 7
  • 53
  • 88