Linux C + + code calls PHP code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.