Quick development of a PHP extension (SO component) tutorial this article explains how to create a PHP5.2 environment extension (PHPExtension) in a very fast way ), I hope that the production process can be understood by anyone who wants to learn quickly in the form of text and text. Requirement: for example, to develop an extension called lanhaicode, enter a string in the extension function lanhai_test (), and return the function: Yourinputst quickly develops a PHP extension (SO component) tutorial.
This article explains how to create a PHP 5.2 Environment Extension (PHP Extension) in a very fast way. I hope that you can learn about the production process in a graphic way.
Requirement: for example, develop an extension called lanhaicode. in the extension, enter a string in the lanhai_test () function, and the function returns: Your input string: xxxxx.
Requirements: understand C/C ++ programming and be familiar with PHP programming.
Environment: Download a php corresponding version of the source code, I here is the php-5.2.17, the first normal installation of php, assuming that our php installed in the/usr/local/php Directory, source code in/root/soft/php/php-5.2.17/, now start!
Php-5.2.17:
Http://blog.lrenwang.com/down/soft/php-5.2.17.tar.bz2
Unzip: tar-vxjf php-5......tar.bz2
Step 1: generate an extension framework
cd /root/soft/php/php-5.2.17/ext./ext_skel --extname=lanhaicodecd /root/soft/php/php-5.2.17/ext/lanhaicodevi config.m4
After opening the file, remove dnl and obtain the following information:
PHP_ARG_ENABLE(lanhaicode, whether to enable lanhaicode support,[ --enable-lanhaicode Enable lanhaicode support])
Save and exit.
Step 2: write code
vi php_lanhaicode.h
Find: PHP_FUNCTION (confirm_lanhaicode_compiled); add a row:
PHP_FUNCTION(lanhai_test);
Save and exit.
vi lanhaicode.c
Add our function to the array, find zend_function_entry lanhaicode_functions [], and add:
PHP_FE(lanhaicode, NULL)
Add the following code at the end of the lanhaicode. c file:
PHP_FUNCTION(lanhai_test){char *arg = NULL;int arg_len, len;char *strg;if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {return;}len = spprintf(&strg, 0, "Your input string: %s\n", arg);RETURN_STRINGL(strg, len, 0);}
Save and exit.
Step 3: compile and install
cd /root/soft/php/php-5.2.17/ext/lanhaicode/usr/local/php/bin/phpize./configure --with-php-config=/usr/local/php/bin/php-configmakemake testmake install
./The following error occurs during configure:
Checking for gcc... no
Checking for cc... no
Checking for cc... no
Checking for cl... no
Configure: error: no acceptable C compiler found in $ PATH
See 'config. log' for more details.
Solution: install the GCC software package and run the following command:
Yum install-y gcc
Now check if there is a/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/lanhaicode. so
Edit php. ini and add the extension:
vi /usr/local/php/lib/php.ini
Add the following in the [PHP] module:
Extension = lanhaicode. so
Save and exit.
Note: If you do not have an extension file directory or the installation reports an error, you can create this directory on your own, copy the extension to the directory, and remember to copy the php. change extension_dir in The ini file to this directory:
Extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613 /"
Step 4: Check the installation result
Now let's see if the module has been loaded:
/Usr/local/php/bin/php-m, which should be printed out as follows:
[PHP Modules]
...
Lanhaicode
...
[Zend Modules]
Restart apache and output phpinfo (). you can see:
Lanhaicode
Lanhaicode support enabled
Check whether the function exists and is called. create lanhaicode. php in the web directory.
";print_r(get_loaded_extensions());print_r(get_extension_funcs('lanhaicode'));echo lanhai_test('My first php extension');echo "
";?>