How to use C to customize PHP extension

Source: Internet
Author: User
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.

  1. PHP_ARG_ENABLE (itbeing,
  2. ?? ? ? ? [Whether to enable the "itbeing" extension],
  3. ?? ? ? ? [? -- Enable-itbeing? ? ? ? Enable "itbeing" extension support])
  4. ?
  5. If test $ PHP_ITBEING! = "No"; then
  6. ?? ? ? ? PHP_SUBST (ITBEING_SHARED_LIBADD)
  7. ?? ? ? ? PHP_NEW_EXTENSION (itbeing, itbeing. c, $ ext_shared)
  8. Fi

2.4 create the php_itbeing.h header file

  1. # Ifndef PHP_ITBEING_H
  2. /* Prevent double insertion Sion */
  3. # Define PHP_ITBEING_H
  4. ?
  5. /* Define extension properties */
  6. # Define PHP_ITBEING_EXTNAME "itbeing"
  7. # Define PHP_ITBEING_EXTVER "1.0"
  8. ?
  9. /* Import configure options
  10. ? * When building outside of
  11. ? * PHP source tree */
  12. # Ifdef HAVE_CONFIG_H
  13. # Include "config. h"
  14. # Endif
  15. ?
  16. /* Include PHP standard Header */
  17. # Include "php. h"
  18. /*
  19. ? * Define the entry point symbole
  20. ? * Zend will use when loading this module
  21. ? */
  22. Extern zend_module_entry itbeing_module_entry;
  23. # Define phpext_itbeing_ptr & itbeing_module_entry
  24. ?
  25. # Endif/* PHP_ITBEING_H */

2.5 create an itbeing. c file

  1. # Include "php_itbeing.h"
  2. ?
  3. PHP_FUNCTION (itbeing_sayhi)
  4. {
  5. ?? ? ? ? Char * name;
  6. ?? ? ? ? Int name_len;
  7. ?
  8. ?? ? ? ? If (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "s ",
  9. ?? ? ? ? ? ? ? ? & Name, & name_len) = FAILURE)
  10. ?? ? ? ? {
  11. ?? ? ? ? ? ? ? ? RETURN_NULL ();
  12. ?? ? ? ?}
  13. ?
  14. ?? ? ? ? Php_printf ("Hi ,");
  15. ?? ? ? ? PHPWRITE (name, name_len );
  16. ?? ? ? ? Php_printf ("! \ N ");
  17. }
  18. ?
  19. Static function_entry php_itbeing_functions [] = {
  20. ?? ? ? ? PHP_FE (itbeing_sayhi, NULL)
  21. ?? ? ? ? {NULL, NULL, NULL}
  22. };
  23. ?
  24. Zend_module_entry itbeing_module_entry = {
  25. # If ZEND_MODULE_API_NO> = 20010901
  26. ?? ? ? ? STANDARD_MODULE_HEADER,
  27. # Endif
  28. ?? ? ? ? PHP_ITBEING_EXTNAME,
  29. ?? ? ? ? Php_itbeing_functions,/* Functions */
  30. ?? ? ? ? NULL,/* MINIT */
  31. ?? ? ? ? NULL,/* MSHUTDOWN */
  32. ?? ? ? ? NULL,/* RINIT */
  33. ?? ? ? ? NULL,/* RSHUTDOWN */
  34. ?? ? ? ? NULL,/* MINFO */
  35. # If ZEND_MODULE_API_NO> = 20010901
  36. ?? ? ? ? PHP_ITBEING_EXTVER,
  37. # Endif
  38. ?? ? ? ? STANDARD_MODULE_PROPERTIES
  39. };
  40. ?
  41. # Ifdef COMPILE_DL_ITBEING
  42. ZEND_GET_MODULE (itbeing)
  43. # 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.