Executing a PHP script from program C and storing the results in a variable

I would like to execute a PHP script from a C program and save the returned content to a C variable.

I tried following, but it does not work:

FROM

printf("calling php function\n");
execl("/usr/bin/php -q", "/var/www/html/phpinfo.php", NULL);
printf("End php function\n");

PHP:

<?php
echo "hello";
?>

Environment:

  • PHP 5.2.6
  • Apache 2.0
  • Fedora Core 10

Also suggest another best way to do this.

+3
source share
3 answers

system() popen(), execl(). , popen(), , execl() . , mumbo jumbo - , , , popen(), !

-...

execl() . , argv[0] main() . , :

execl("/usr/bin/php", "/usr/bin/php", "-q",
    "/var/www/html/phpinfo.php", (char *) NULL);

( (char *), , , 0, NULL 0, (void *) 0, .)

...

execl(), . exec fork() pipe(). , exec ; ! , execl(), . . execl() . , , , , /usr/bin/php.

, fork() pipe()? , , - . "" , execl() /usr/bin/php. , , .

, , Google . -, (!) , fork/exec.

. , , , . popen(), , child stderr stdin stdout.

...

#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

pid_t execute(const char *command, FILE **in, FILE **out, FILE **err)
{
    pid_t pid;
    int   fd[6];

    pipe(&fd[0]);
    pipe(&fd[2]);
    pipe(&fd[4]);

    switch (pid = fork()) {
        case -1:
            perror("unable to fork()");
            exit(1);

        case 0:
            close(fd[1]);   // Close write end of stdin.
            close(fd[2]);   // Close read end of stdout.
            close(fd[4]);   // Close read end of stderr.

            dup2(fd[0], STDIN_FILENO);  // Have stdin read from the first pipe.
            dup2(fd[3], STDOUT_FILENO); // Have stdout write to the second pipe.
            dup2(fd[5], STDERR_FILENO); // Have stderr write to the third pipe.

            execlp("/bin/sh", "/bin/sh", "-c", command, (char *) NULL);

            perror("execlp() failed");
            _exit(1);

        default:
            close(fd[0]); // Close read end of stdin.
            close(fd[3]); // Close write end of stdout.
            close(fd[5]); // Close write end of stderr.

            if (in)  *in  = fdopen(fd[1], "wb"); else close(fd[1]);
            if (out) *out = fdopen(fd[2], "rb"); else close(fd[2]);
            if (err) *err = fdopen(fd[4], "rb"); else close(fd[4]);

            return pid;
    }
}
+12

, popen: ( ):

popen() pclose() ls *, :

#include <stdio.h>
...


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() */
    ...
}

, PHP script C, , , , , , , .

+7

, execl(), " ". , "End php function", .

, system fork() execl exec.

+1

Source: https://habr.com/ru/post/1711317/


All Articles