php-internal-docs 8.4.8
Unofficial docs for php/php-src
|
Files | |
php_embed.c | |
php_embed.h | |
A server application programming interface (SAPI) is the entry point into the Zend Engine. The embed SAPI is a lightweight SAPI for calling into the Zend Engine from C or other languages that have C bindings.
Below is a basic example in C that uses the embed SAPI to boot up the Zend Engine, start a request, and print the number of functions loaded in the function table.
To compile this, we must point the compiler to the PHP header files. The paths to the header files are listed from php-config --includes
.
We must also point the linker and the runtime loader to the libphp.so
shared lib for linking PHP (-lphp
) which is located at $(php-config --prefix)/lib
. So the complete command to compile ends up being:
:memo: The embed SAPI is disabled by default. In order for the above example to compile, PHP must be built with the embed SAPI enabled. To see what SAPIs are installed, run
php-config --php-sapis
. If you don't seeembed
in the list, you'll need to rebuild PHP with./configure --enable-embed
. The PHP shared librarylibphp.so
is built when the embed SAPI is enabled.
If all goes to plan you should be able to run the program.
The following example calls mt_rand()
and var_dump()
s the return value.
The default value for 'error_prepend_string' is 'NULL'. The following example sets the INI default for 'error_prepend_string' to 'Embed SAPI error:'.
After compiling and running, you should see:
This default value is overwritable from INI files. We'll update one of the INI files (which can be found by running $ php --ini
), and set error_prepend_string="Oops!"
. We don't have to recompile the program, we can just run it again and we should see: