2

I can find a lot of information on running C++ programs from PHP, but not the other way around. I want to run a PHP script from C++, which will perform some calculations and then use "echo" for output. The C++ program should wait to receive the output from the PHP script and parse it before continuing.

Thanks!

4
  • On which operating system? Commented Dec 30, 2013 at 21:19
  • Windows 7, 64-bit if it matters. Commented Dec 30, 2013 at 21:20
  • 2
    As you are under Windows, consider reading this: msdn.microsoft.com/en-us/library/windows/desktop/… Launch the PHP CLI interface and redirect stdout Commented Dec 30, 2013 at 21:21
  • I'll take a look at it. I've got some experience with different languages and just picked up C++ last night and learned the syntax quick because I think this is the best way to go about doing what I need to do. Commented Dec 30, 2013 at 21:23

2 Answers 2

2

Try this

#include <cstdlib>
int main(){
   system("php file.php");
   return 0;
}
Sign up to request clarification or add additional context in comments.

4 Comments

This does not deal with the output
Actually if I could get this to work I could just write to a file for output and deal with it that way, I'm brand new to C++ and would prefer not to have to dredge through the link you left me at this time. So if you can tell me how to make this guy's response work, I'll modify my PHP file accordingly and accept your answer!
It's a horrible solution, however you just have to add #include <cstdlib> before everything else, and ensure php is in the PATH. Please consider following my advice (which is identical to the one of Dieter Lucking)
I totally recognize why it's a horrible solution, but I need it for a very specific situation and I will make it work. Thanks!
1

You could utilize a pipe and run 'php script.php'. An annoying example for a pipe in windows (full of boilerplate) is at http://msdn.microsoft.com/en-us/library/windows/desktop/ms682499%28v=vs.85%29.aspx

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.