6

I'm attempting to call a PHP script from a C++ program. For instance, here is an example C++ program:

#include <iostream>
#include <cstdlib>

using namespace std; 

int main() {

cout << std::system("test.php");

return 0;
}

This program is calling some script "test.php" which might be structured like this:

<?php
echo "Hello";
?>

When running the C++ code, I get the following:

sh: 1: test.php: not found.

Now the obvious thing to check is if the files are in the same directory (they indeed are), however the error still persists. Any feedback on how I might go about doing something like this?

Thanks.

4
  • 4
    If nothing else, it has to be php test.php Commented Feb 27, 2012 at 18:47
  • Jeez, what a dumb mistake on my part. Go ahead and leave an actual response, and I'll make sure to select your answer in case anyone else has the same problem. (Although due to the simplicity, this question may be closed as well). Commented Feb 27, 2012 at 18:49
  • 2
    Go ahead and give the points to someone else, glad I could help! :-) Commented Feb 27, 2012 at 18:51
  • 2
    Well thanks a ton either way. :) Commented Feb 27, 2012 at 18:53

2 Answers 2

6

Try:

cout << std::system("php -f test.php");

If you want to execute a php script as a command line script you must add a shebang line at the first line of you php script. Like

#!/usr/bin/php
Sign up to request clarification or add additional context in comments.

Comments

2

You have to specify the program which shall run the script ("php" in your case), unless the file is marked as executable, belongs to a directory of the PATH environment variable (or you run ./test.php) and has a shebang. You have to use system("php test.php").

This is because "sh" searches for "test.php" in the directories specified by PATH and the working directory usually is not contained in PATH.

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.