Home > Article > Backend Development > Developing PHP extensions with C Example (Basic Edition)_PHP Tutorial
Step one: Create an extended skeleton.
[html]
cd /usr/local/src/php-5.3.6/ext/
./ext_skel --extname=laiwenhui
cd /usr/local/src/php-5.3.6/ext/
./ext_skel --extname=laiwenhui Step 2: Modify the compilation parameters.
[html] view plaincopyprint?cd php-5.3.6/ext/laiwenhui
vi config.m4
cd php-5.3.6/ext/laiwenhui
Remove vi config.m4
PHP_ARG_ENABLE(laiwenhui, whether to enable laiwenhui support,
[ --enable-laiwenhui Enable laiwenhui support])
dnl
in front of two lines
Modified to:
dnl Otherwise use enable:
PHP_ARG_ENABLE(laiwenhui, whether to enable laiwenhui support,
dnl Make sure that the comment is aligned:
[ --enable-laiwenhui Enable laiwenhui support])
Step 3: Write code
[html]
vim php_laiwenhui.h
vim php_laiwenhui.h
in
PHP_FUNCTION(confirm_laiwenhui_compiled);
Add a new line after
PHP_FUNCTION(test);
[html]
vim laiwenhui.c
vim laiwenhui.c
at
PHP_FE(confirm_laiwenhui_compiled, NULL)
Add after
PHP_FE(test, NULL)
**Add the following code at the end of the file:**
[html]
PHP_FUNCTION(test)
{
char *arg = "This is my first extention!";
int len;
char *strg;
len = spprintf(&strg, 0, "%sn", arg);
RETURN_STRINGL(strg, len, 0);
}
PHP_FUNCTION(test)
{
char *arg = "This is my first extention!";
int len;
char *strg;
len = spprintf(&strg, 0, "%sn", arg);
RETURN_STRINGL(strg, len, 0);
}Step 4: Compile the code
[html]
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
My PHP installation path is: /usr/local/php
At this time, a file /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/laiwenhui.so
Edit the PHP configuration file php.ini and add the extension:
[html]
vim php.ini
vim php.ini is added under the [PHP] module:
[html]
extension = laiwenhui.so
extension = laiwenhui.so change the extension_dir in the php.ini file to this directory:
[html]
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
Step 5: Check the installation results
1. Restart php-fpm, ngixn
2./use/local/php/bin/php -m
Check to see if the laiwenhui extension is included.
Step 6: Execute the test code
Create test.php
in the website root directory
vim test.php
The code content is as follows
echo test();
The result after execution is:
This is my first extention!
If you can successfully complete the above steps, congratulations on completing your first extension.