How to use C to customize PHP extension because some code requires encryption and decryption, the PHP module needs to be extended. Therefore, base64 is used to implement simple encryption algorithms. Because of the time relationship, this article mainly provides an overview and record on how to implement PHP extension, and does not involve the specific implementation of the encryption algorithm. if you have time, add it. How to use C to customize PHP extension
Because some code requires encryption and decryption, the PHP module needs to be extended. Therefore, base64 is used to implement simple encryption algorithms. Because of the time relationship, this article mainly provides an overview and record on how to implement PHP extension, and does not involve the specific implementation of the encryption algorithm. if you have time, add it.
1. environment:
Centos 5
Php 5.1.6
Autoconf 2.59
Automake 1.96
Libtool
Bison
Flex
Re2c
2. create a module
2.1 go to the php source code directory extension package directory
Cd/usr/include/php/ext
2.2 create a folder named itbeing (here our module name is called itbeing)
Mkdir itbeing
Cd itbeing
2.3 create config. m4 file, config. the m4 file is written using the GNU autoconf syntax. The main function of this file is to tell the system which extension configure options are supported for building the system and which extension libraries are needed, and which source files need to be compiled as part of it.
- PHP_ARG_ENABLE (itbeing,
- ?? ? ? ? [Whether to enable the "itbeing" extension],
- ?? ? ? ? [? -- Enable-itbeing? ? ? ? Enable "itbeing" extension support])
- ?
- If test $ PHP_ITBEING! = "No"; then
- ?? ? ? ? PHP_SUBST (ITBEING_SHARED_LIBADD)
- ?? ? ? ? PHP_NEW_EXTENSION (itbeing, itbeing. c, $ ext_shared)
- Fi
2.4 create the php_itbeing.h header file
- # Ifndef PHP_ITBEING_H
- /* Prevent double insertion Sion */
- # Define PHP_ITBEING_H
- ?
- /* Define extension properties */
- # Define PHP_ITBEING_EXTNAME "itbeing"
- # Define PHP_ITBEING_EXTVER "1.0"
- ?
- /* Import configure options
- ? * When building outside of
- ? * PHP source tree */
- # Ifdef HAVE_CONFIG_H
- # Include "config. h"
- # Endif
- ?
- /* Include PHP standard Header */
- # Include "php. h"
- /*
- ? * Define the entry point symbole
- ? * Zend will use when loading this module
- ? */
- Extern zend_module_entry itbeing_module_entry;
- # Define phpext_itbeing_ptr & itbeing_module_entry
- ?
- # Endif/* PHP_ITBEING_H */
2.5 create an itbeing. c file
- # Include "php_itbeing.h"
- ?
- PHP_FUNCTION (itbeing_sayhi)
- {
- ?? ? ? ? Char * name;
- ?? ? ? ? Int name_len;
- ?
- ?? ? ? ? If (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "s ",
- ?? ? ? ? ? ? ? ? & Name, & name_len) = FAILURE)
- ?? ? ? ? {
- ?? ? ? ? ? ? ? ? RETURN_NULL ();
- ?? ? ? ?}
- ?
- ?? ? ? ? Php_printf ("Hi ,");
- ?? ? ? ? PHPWRITE (name, name_len );
- ?? ? ? ? Php_printf ("! \ N ");
- }
- ?
- Static function_entry php_itbeing_functions [] = {
- ?? ? ? ? PHP_FE (itbeing_sayhi, NULL)
- ?? ? ? ? {NULL, NULL, NULL}
- };
- ?
- Zend_module_entry itbeing_module_entry = {
- # If ZEND_MODULE_API_NO> = 20010901
- ?? ? ? ? STANDARD_MODULE_HEADER,
- # Endif
- ?? ? ? ? PHP_ITBEING_EXTNAME,
- ?? ? ? ? Php_itbeing_functions,/* Functions */
- ?? ? ? ? NULL,/* MINIT */
- ?? ? ? ? NULL,/* MSHUTDOWN */
- ?? ? ? ? NULL,/* RINIT */
- ?? ? ? ? NULL,/* RSHUTDOWN */
- ?? ? ? ? NULL,/* MINFO */
- # If ZEND_MODULE_API_NO> = 20010901
- ?? ? ? ? PHP_ITBEING_EXTVER,
- # Endif
- ?? ? ? ? STANDARD_MODULE_PROPERTIES
- };
- ?
- # Ifdef COMPILE_DL_ITBEING
- ZEND_GET_MODULE (itbeing)
- # Endif
3. compilation module
3.1 phpize
3.2./config-enable-itbeing
3.3 make
3.4 cp modules/itbeing. so/usr/lib/php/modules
3.5 vim/etc/php. ini add extension = itbeing. so
Test: php-r "itbeing_sayhi ('kokko ')"
Result: Hi, kokko
?
Original article: http://www.kokkowon.com/archives/981