You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
4
+
5
+
## Basic Example
6
+
7
+
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.
8
+
9
+
```c
10
+
/* embed_sapi_basic_example.c */
11
+
12
+
#include<sapi/embed/php_embed.h>
13
+
14
+
intmain(int argc, char **argv)
15
+
{
16
+
/* Invokes the Zend Engine initialization phase: SAPI (SINIT), modules
17
+
* (MINIT), and request (RINIT). It also opens a 'zend_try' block to catch
18
+
* a zend_bailout().
19
+
*/
20
+
PHP_EMBED_START_BLOCK(argc, argv)
21
+
22
+
php_printf(
23
+
"Number of functions loaded: %d\n",
24
+
zend_hash_num_elements(EG(function_table))
25
+
);
26
+
27
+
/* Close the 'zend_try' block and invoke the shutdown phase: request
28
+
* (RSHUTDOWN), modules (MSHUTDOWN), and SAPI (SSHUTDOWN).
29
+
*/
30
+
PHP_EMBED_END_BLOCK()
31
+
}
32
+
```
33
+
34
+
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`.
35
+
36
+
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:
37
+
38
+
```bash
39
+
$ gcc \
40
+
$(php-config --includes) \
41
+
-L$(php-config --prefix)/lib \
42
+
embed_sapi_basic_example.c \
43
+
-lphp \
44
+
-Wl,-rpath=$(php-config --prefix)/lib
45
+
```
46
+
47
+
> :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 see `embed` in the list, you'll need to rebuild PHP with `./configure --enable-embed`. The PHP shared library `libphp.so` is built when the embed SAPI is enabled.
48
+
49
+
If all goes to plan you should be able to run the program.
50
+
51
+
```bash
52
+
$ ./a.out
53
+
Number of functions loaded: 1046
54
+
```
55
+
56
+
## Function call example
57
+
58
+
The following example calls `mt_rand()` and `var_dump()`s the return value.
/* Generates an error by accessing an undefined variable '$a'. */
144
+
if (zend_eval_stringl(ZEND_STRL("var_dump($a);"), &retval, "example") == FAILURE) {
145
+
php_printf("Failed to eval PHP.\n");
146
+
}
147
+
148
+
PHP_EMBED_END_BLOCK()
149
+
}
150
+
```
151
+
152
+
After compiling and running, you should see:
153
+
154
+
```
155
+
Embed SAPI error:
156
+
Warning: Undefined variable $a in example on line 1
157
+
NULL
158
+
```
159
+
160
+
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:
161
+
162
+
```
163
+
Oops!
164
+
Warning: Undefined variable $a in example on line 1
0 commit comments