11

I have a C++ program that needs to take data from a PHP script, process it, and return the data to my PHP script.

  1. How do you pass the values from PHP to C++?
  2. How do you run the C++ script? Do you have to compile it first some how?
  3. How do you get the values out of the C++ script?
Don P
  • 60,113
  • 114
  • 300
  • 432

2 Answers2

22

1 . How do you pass the values from PHP to C++?

Ans: In php file, you could use exec function to execute your C++ binary file. Example:

exec("/path/to/your/binary $var1 $var2", $output);

2 . How do you run the C++ script? Do you have to compile it first some how?

Ans: Of course, you can't execute the C++ script directly, C++ is compiled language, you could just execute the binary file.

3 . How do you get the values out of the C++ script?

Ans: See 1, you will get the output form C++ by $output.

xdazz
  • 158,678
  • 38
  • 247
  • 274
  • Since this question is likely being viewed by people new to calling the command line from inside PHP I think a word of caution about command injection is warranted. When using this method as part of a website you have to be careful that someone won't insert commands into your variables (i.e. $var1 $var2 in the example) if you're getting input from the user. – James Jun 16 '17 at 19:43
2

1- you can use exec to call external application, use command line parameters

2- C++ is not scripting, its a compiled language, you have to compile it first

3- normal std out will be captured by php exec function

Hawili
  • 1,649
  • 11
  • 15
  • if you want to use webserver with c++ you have to check cgi, then php will use a normal web call to call c++ application and get the results using something like `curl` – Hawili Aug 03 '12 at 04:08