Skip to content

Instantly share code, notes, and snippets.

@brzuchal
Created April 18, 2017 08:03
Show Gist options
  • Select an option

  • Save brzuchal/7ec7def2b4fb809773ca93cf4d7268a1 to your computer and use it in GitHub Desktop.

Select an option

Save brzuchal/7ec7def2b4fb809773ca93cf4d7268a1 to your computer and use it in GitHub Desktop.
Embeding PHP7
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sapi/embed/php_embed.h>
#include <Zend/zend_stream.h>
#include <Zend/zend.h>
int main(int argc, char *argv[])
{
zend_file_handle script;
FILE *fp;
if (argc != 2) {
fprintf(stderr, "Usage: embed filename.php\n");
return -1;
}
if ((fp = fopen(argv[1], "rb")) == NULL) {
fprintf(stderr, "Unable to open %s\n", argv[1]);
return -1;
}
fclose(fp);
argc--;
argv++;
PHP_EMBED_START_BLOCK(argc, argv)
while (1) {
// Do some 'work'
script.type = ZEND_HANDLE_FP;
script.filename = argv[0];
script.opened_path = NULL;
script.free_filename = 0;
if ((script.handle.fp = fopen(script.filename, "rb")) == NULL) {
fprintf(stderr, "Unable to open %s\n", argv[1]);
return -1;
}
php_execute_script(&script TSRMLS_CC);
//zend_file_handle_dtor(&script);
}
PHP_EMBED_END_BLOCK()
return 0;
}
CC=gcc
CFLAGS=-c $(shell php-config --includes ) -Wall -g
LDFLAGS=$(shell php-config --ldflags )
all: embed.c
$(CC) -o embed.o embed.c $(CFLAGS)
$(CC) -o embed embed.o $(LDFLAGS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment