executing php script from C program and store the results in to a variable

前端 未结 3 1550
半阙折子戏
半阙折子戏 2021-01-03 09:47

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:

3条回答
  •  天命终不由人
    2021-01-03 10:03

    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.

提交回复
热议问题