In Linux, CC ++ Code calls PHP code. in Linux, C/C ++ code calls PHP code through the popen system function and uses the fgets function to obtain the echo output string of PHP code.
// Main. cchar str [1024] = {0}; char * cmd = "php/src/test/c. php 1234 "; FILE * stream = NULL; if (stream = popen (cmd," r ") = NULL) {// use popen to execute the PHP code return "";} std: string ret = ""; while (fgets (str, 1024, stream ))! = NULL) {// use fgets to obtain the echo output string ret + = str;} pclose (stream); return ret;
The "1234" in cmd is the parameter passed to the PHP file. In PHP code, like the main function in common C language, argc and argv can be used to obtain the parameter "1234 ".
/// Src/test/c. php
In PHP code, it should be noted that if the called php file has an extra php file include, it is best to use the absolute path of the php file through dirname (_ FILE _) or other methods, otherwise, the PHP file cannot be found.