I would like to execute a PHP script from a C program and store the returning content in to a C variable.
I tried like following but it doesn\'t work:
Probably the easiest way would be to use the popen function: (excerpt from linked page):
The following example demonstrates the use of popen() and pclose() to execute the command ls * in order to obtain a list of files in the current directory:
#include
...
FILE *fp;
int status;
char path[PATH_MAX];
fp = popen("ls *", "r");
if (fp == NULL)
/* Handle error */;
while (fgets(path, PATH_MAX, fp) != NULL)
printf("%s", path);
status = pclose(fp);
if (status == -1) {
/* Error reported by pclose() */
...
} else {
/* Use macros described under wait() to inspect `status' in order
to determine success/failure of command executed by popen() */
...
}
However, wanting to read the output of a PHP script from within a C program is kind of red flag, and I suspect there is probably some cleaner overall solution to this, but it's hard to say more without a higher level description of what you're trying to do.