Calling a PHP script from a C ++ program

I am trying to call a PHP script from a C ++ program. For example, here is an example C ++ program:

#include <iostream> #include <cstdlib> using namespace std; int main() { cout << std::system("test.php"); return 0; } 

This program calls some script "test.php", which can be structured as follows:

 <?php echo "Hello"; ?> 

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

 sh: 1: test.php: not found. 

Now itโ€™s obvious to check if the files are in the same directory (they really are), however the error still persists. Any feedback on how I can do something like this?

Thanks.

+4
source share
2 answers

Try:

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

If you want to execute a php script as a command line script, you must add the shebang line in the first line of the PHP script. how

 #!/usr/bin/php 
+5
source

You must specify the program that should run the script ("php" in your case) if the file is not marked as an executable file, belongs to the PATH environment variable directory (or you run ./test.php ) and has shebang. You should use system("php test.php") .

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

+2
source

Source: https://habr.com/ru/post/1398665/


All Articles