在php7嵌入式开发–环境配置基础上,我们新建个embed1.c,代码如下:
#include <stdio.h> #include <sapi/embed/php_embed.h> int main(int argc, char *argv[]) { zend_file_handle script; if ( argc <= 1 ) { fprintf(stderr, "Usage: %s <filename.php> <arguments>\n", argv[0]); return -1; } script.type = ZEND_HANDLE_FP; script.filename = argv[1]; script.opened_path = NULL; script.free_filename = 0; if ( !(script.handle.fp = fopen(script.filename, "rb")) ) { fprintf(stderr, "Unable to open: %s\n", argv[1]); return -1; } argc --; argv ++; PHP_EMBED_START_BLOCK(argc, argv) php_execute_script(&script TSRMLS_CC); PHP_EMBED_END_BLOCK() return 0; }
编译命令如下:
gcc -I /usr/local/include/php/ -I /usr/local/include/php/main/ -I /usr/local/include/php/Zend/ -I /usr/local/include/php/TSRM/ -lphp7 -o embed1 embed1.c
执行如下测试
这样,我们就可以通过embed1执行PHP源代码了,相当于php下的cli.