Write PHP extensions in C Language
1: pre-defined
In the home directory, you can also write a file in any other directory, such as caleng_module.def.
The content is the name and parameters of the function you want to define:
Int A (int x, int y)
String B (string STR, int N)
2: Go to the ext directory of the PHP source code directory
# Cd/usr/local/php-5.4.0/EXT/
Run the command to generate the corresponding extension directory
#./Ext_skel -- extname = caleng_module -- proto =/home/hm/caleng_module.def
3: Modify config. M4
Remove DNL comments
Php_arg_enable (caleng_module, whether to enable caleng_module support,
Make sure that the comment is aligned:
[-Enable-caleng_module enable caleng_module support])
4: Modify caleng_module.c
/* {{{ proto int a(int x, int y) */PHP_FUNCTION(a){int argc = ZEND_NUM_ARGS();int x;int y; int z;if (zend_parse_parameters(argc TSRMLS_CC, "ll", &x, &y) == FAILURE) return;z=x+y;RETURN_LONG(z);}/* }}} *//* {{{ proto string b(string str, int n) */PHP_FUNCTION(b){char *str = NULL; int argc = ZEND_NUM_ARGS(); int str_len; long n; char *result; char *ptr; int result_length; if (zend_parse_parameters(argc TSRMLS_CC, "sl", &str, &str_len, &n) == FAILURE) return; result_length = str_len * n; result = (char *) emalloc(result_length + 1); ptr = result; while (n--) { memcpy(ptr, str, str_len); ptr += str_len; } *ptr = '\0'; RETURN_STRINGL(result, result_length, 0);}/* }}} */
5. generate an extension Library
# Cd./caleng_module
#/Usr/local/PHP/bin/phpize
#./Configure -- With-PHP-Config =/usr/local/PHP/bin/PHP-config
# Make
# Make install
6: corresponding extensions directory to PhP
As shown in
# Cd/usr/local/PHP/lib/PHP/extend/no-debug-non-zts-20100525/
The generated caleng_module.so file is in the directory.
7: Modify PHP. ini
If the php. ini file cannot be found, you can view the phpinfo () file.
# Cd/usr/local/PHP/lib/
Added extension information for PHP. ini.
Extension = caleng_module.so
8: restart Apache
#/Usr/local/apache2/bin/apachectl restart
9: Check Loading
/Usr/local/PHP/bin/PHP-m
10: PhP call
echo a(1,2);
Output 3 indicates the operation is successful!
======================================
Below is the original text
Linux with C Development PHP extension, first download PHP source package, assuming the source package directory is:/software/php-5.2.13
First download the PHP source package, assuming the source package directory is:/software/php-5.2.13
#> Cd/software/php-5.2.13/EXT
2. Suppose we want to develop an extension named caleng_module, which contains two functions: A -- processing two integers and B-processing string repeated output;
1. First, write a function definition file. In this file, the prototype Suffix of the function is def, which is assumed to be caleng_module.def.
Int A (int x, int y)
String B (string STR, int N)
2. The extension directory caleng_module will be automatically created under the ext directory through the extension skeleton generator.
#>./Ext_skel -- extname = caleng_module -- proto = caleng_module.def
3. modify the configuration file: #> Vim/software/php-5.2.13/EXT/caleng_module/config. M4, remove the comment label "DNL" in the following line, after the modification is as follows:
Php_arg_enable (myfunctions, whether to enable myfunctions support,
Make sure that the comment is aligned:
[-- Enable-myfunctions enable myfunctions support])
4. Improve functions of functions A and B: #> Vim/software/php-5.2.13/EXT/caleng_module/caleng_module.c
Php_function ()
{
Int x, y, z;
Int argc = zend_num_args ();
If (zend_parse_parameters (argc tsrmls_cc, "ll", & X, & Y) = failure)
Return;
Z = x + y;
Return_long (z );
}
Php_function (B)
{
Char * STR = NULL;
Int argc = zend_num_args ();
Int str_len;
Long N;
Char * result;
Char * PTR;
Int result_length;
If (zend_parse_parameters (argc tsrmls_cc, "SL", & STR, & str_len, & n) = failure)
Return;
Result_length = str_len * N;
Result = (char *) emalloc (result_length + 1 );
PTR = result;
While (n --){
Memcpy (PTR, STR, str_len );
PTR + = str_len;
}
* PTR = '\ 0 ';
Return_stringl (result, result_length, 0 );
}
3. Compile and install PHP. Assume that the installation directory of PHP is/usr/localhost/webserver/PHP.
#> Cd/software/php-5.2.13/EXT/caleng_module
#>/Usr/localhost/webserver/PHP/bin/phpize
#>./Configure -- With-PHP-Config =/usr/localhost/webserver/PHP/bin/PHP-config
#> Make
#> Make install
Now the caleng_module.so file is generated in the/usr/local/webserver/PHP/lib/PHP/extensions/no-debug-non-zts-20060613 directory
Add extension = caleng_module.so to the php. ini configuration file.
Work done
Http://blog.csdn.net/heiyeshuwu/article/details/3453854 for more information
Http://blog.csdn.net/taft/article/details/596291
Http://blog.sina.com.cn/s/blog_638f9da701012zk3.html