under Linux, C + + code can invoke PHP code through the Popen system function and get a string of the echo output of the PHP code via the Fgets function.
Main.cchar str[1024] = {0};char * cmd = "php/src/test/c.php 1234"; FILE * stream = null;if (stream = Popen (cmd, "r")) = = = NULL) {//execute PHP code by popen return "";} std::string ret = "", while ((Fgets (str, 1024x768, stream)) = NULL) {//fgets gets the string of echo output in PHP ret + = str;} Pclose (stream); return ret;
where cmd "1234" is the parameter passed to the PHP file. PHP code, like the normal C language of the main function, through the ARGC and argv the 2 parameters to get this parameter "1234".
/src/test/c.php<?phpinclude_once (DirName (__file__). ' /m.php '); Include_once (DirName (__file__). ' /m/spstable.php '); if ($ARGC! = 2) {//and C + + 's main function, there are 2 parameters, the first is the PHP file name, the second is the "1234" Die () in the CMD string in main.c);} Array_shift ($ARGV);//Remove the first parameter, the first parameter is the PHP file name $spsfileid = (int) $argv [0];
In the PHP code, it is important to note that if the PHP file being called has include additional PHP files, it is best to use DirName (__file__) or other methods of PHP file absolute path, otherwise it is easy to find the PHP file error.
Linux C + + code calls PHP code