Follow these steps to write PHP extensions using the C language and PHP extension writing format (Zend API:
To the PHP installation directory
[Root @ test1 ext] # cd/root/PHP/php5.2/EXT
[Root @ test1 ext] #./ext_skel-extname = cltest
Modify config. M4
[Root @ test1 ext] # vi cltest/config. M4
Delete 3 DNL
DNL php_arg_with (my_module, for my_module support,
DNL make sure that the comment is aligned:
DNL [-- with-my_module include my_module support])
Or delete the three DNL
DNL php_arg_enable (my_module, whether to enable my_module support,
DNL make sure that the comment is aligned:
DNL [-enable-my_module enable my_module support])
Modification completed.
Add the PHP function definition to the main program file of the my_module.c extension module.
The main program describes the declaration of the PHP extension module, the number of functions contained in the module, the role of each function, the content displayed in the phpinfo function, the module initialization, and the end
Everything will be described in this file. We just added a function say_hello and described the specific content of the say_hello function.
[Root @ test1 ext] # vi cltest. c
Function_entry my_module_functions [] = {
Php_fe (say_hello, null )/*? Add this line of code to register the function say_hello ()*/
Php_fe (confirm_my_module_compiled, null)/* for testing, remove later .*/
{Null, null, null}/* must be the last line in my_module_functions [] */
};
Add the following code function program subject at the end of the file
Php_function (say_hello)
{
Return_stringl ("Hello World );
}
Modification completed.
Add the function to be registered when PHP is started in the header file of the my_module.h extension module.
In the corresponding header file, declare the say_hello function to complete our expected functions.
[Root @ test1 ext] # vi php_cltest.h
In the file php_function (confirm_my_module_compiled); Add the following code function definition before a line
Php_function (say_hello );
Save the file and exit. The modification is complete.
Find the execution file phpize and run the command in the cltest directory to load the dynamic link library.
[Root @ test1 cltest] #/usr/local/PHP/bin/phpize
Note the version of apxs, using the-with-apxs or-with-apxs2 command
[Root @ test1 cltest] # Find/-name apxs-print
Execute the compilation command
[Root @ test1 cltest] #. /configure-enable-cltest-with-apxs =/usr/local/Apache/bin/apxs-with-PHP-Config =/usr/local/PHP/bin/PHP-config
// This part below is intended to call other dynamic link libraries of C, and has not been tested successfully yet.
Copy php_test.so to/usr/lib/
# Include "php_test.h"
Directly use the strtoupper () interface provided by php_test.so ().
[Root @ test1 cltest] #. /configure-enable-cltest ldflags =-lphp_test-with-apxs =/usr/local/Apache/bin/apxs-with-PHP-Config =/usr/local/PhP6/bin /PHP-config
Add libtest. A to extra_libs In The makefile of the extension library of PHP.
// The above part is intended to call other dynamic link libraries of C, and the test has not been successful yet.
[Root @ test1 cltest] # Make
A directory named modules will be generated under the current directory, and the cltest. So file you want will be placed below it.
[Root @ test1 cltest] # cp modules/cltest. So/usr/local/PHP/include/PHP/EXT/
Here, you need to first set your PHP extension directory to search for "extension_dir" in PHP. ini"
Find the PHP Execution file/usr/local/PHP/bin/PHP
[Root @ test1 ext] # Find/-name PHP-print
[Root @ test1 ext] #./PHP-F/root/PHP/php5.2/EXT/cltest. php // test whether the load is successful
Open the extension/usr/local/PHP/lib/PHP. ini in the PHP. ini file.
Extension = jinzhesheng_module.so
Then restart Apache
Sudo/usr/sbin/apachectl stop
Sudo/usr/sbin/apachectl start
[Root @ test1 ext] # Find/-name apachectl-print
[Root @ test1 ext] #/apachectlpath/apachectl stop
[Root @ test1 ext] #/apachectlpath/apachectl start
Use phpinfo to check
After you modify the cltest. c file, run the make command and restart Apache to update the dynamic link library.
For example, add the chenlong function.
[Root @ test1 cltest] # vi EXT/cltest. c
Added: php_fe (chenlong, null)
Php_function (chenlong)
{
// With Parameters
Zval ** yourname;
If (zend_num_args ()! = 1 | zend_get_parameters_ex (1, & yourname) = failure)
{
Wrong_param_count;
}
Zend_printf ("Hello world, % s \ n", z_strval_pp (yourname ));
}
[Root @ test1 cltest] # vi php_cltest.h
Added: php_function (chenlong );
[Root @ test1 cltest] # Make
[Root @ test1 cltest] # cp modules/ctest. So/usr/local/PHP/include/PHP/EXT/
[Root @ test1 ext] #/apachectlpath/apachectl stop
[Root @ test1 ext] #/apachectlpath/apachectl start
Test:
<?
Say_hello ();
Chenlong ("dragonchen ");
?>
In the MAKEFILE file:
Extra_cflags =-I/usr/local/bind/include
Extra_ldflags =-L/usr/local/bind/lib
Extra_libs =-lbind
CP modules/ctest. So/usr/local/PHP/include/PHP/EXT/
/Usr/sbin/apachectl stop
/Usr/sbin/apachectl start