C/C ++ code in Linux calls PHP code

Source: Internet
Author: User
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.

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.