怎麼利用C自訂實現PHP擴充

來源:互聯網
上載者:User
如何利用C自訂實現PHP擴充

由於有一部分代碼需要加解密,所以需要擴充PHP模組,於是簡單的使用base64來實現簡單的密碼編譯演算法。因為時間的關係,這裡主要是對如何?PHP擴充做一個概述和記錄,並不涉及到密碼編譯演算法的具體實現,等有空再補上。

1、環境:
centos 5
php 5.1.6
autoconf 2.59
automake 1.96
libtool
bison
flex
re2c

2、建立模組
2.1 轉到php源碼目錄擴充包目錄下
cd /usr/include/php/ext

2.2 建立一個叫做itbeing的檔案夾(這裡我們的模組名稱就叫做itbeing了)
mkdir itbeing
cd itbeing

2.3 建立config.m4檔案,config.m4 檔案使用 GNU autoconf 文法編寫,該檔案的主要作用是 檔案告訴系統構建系統哪些擴充 configure 選項是支援的,你需要哪些擴充庫,以及哪些源檔案要編譯成它的一部分。

  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 建立php_itbeing.h 標頭檔

  1. #ifndef PHP_ITBEING_H
  2. /* Prevent double inclusion */
  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 the
  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 建立itbeing.c 檔案

  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、編譯模組
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 添加extension = itbeing.so

測試:php -r “itbeing_sayhi(’kokko’)”
結果:Hi,kokko

?

原文:http://www.kokkowon.com/archives/981

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.