我有一段代码,工作得很好。如何运行一点PHP代码C
FILE *pipe_fp;
if((pipe_fp = popen("php", "w")) == NULL) {
perror("popen(PHP)");
exit(1);
}
fputs("<?php echo process('xx'); ?>", pipe_fp);
int ret = pclose(pipe_fp);
if(WIFEXITED(ret))
printf("%d\n", WEXITSTATUS(ret));
问题是,当我尝试这样:
// up to this point i am starting a socket servers and wait for clients
int bSize;
int nSize;
char buffer[MAXBUF+1];
char receive[MAXBUF+1];
while(1) {
bSize = recv(new_fd, buffer, MAXBUF, 0);
if(bSize > 0) {
buffer[bSize] = '\0';
strcat(receive, buffer);
}
}
// I rote this part based on this post: http://stackoverflow.com/questions/1383649/concatenating-strings-in-c-which-method-is-more-efficient
char * first= "<?php echo process('";
char * second = "'); ?>";
char * code = NULL;
asprintf(&code, "%s%s%s", first, receive, second);
// the above code somes here, i just copied the part that has changed
fputs(code, pipe_fp);
我已经尝试了一堆所有导致失败的其他例子。 我在C 3天。
http://stackoverflow.com/questions/705443/how-to-mix-up-c-and-php 是否链接帮助? – Friedrich
Apache httpd是开源的,就像PHP一样。只要看看源代码。 –
可能是有一个PHP库,它提供了apis来执行php脚本 – MOHAMED