利用C/C++扩展php语言实现 Usher_GetHostIP无参函数

PHP程序员需要略会C也是必要的,不管从业务角度还是 专业角度,因为C是PHP的母语。并且我们一般不会用原生PHP来处理大批量业务逻辑,这个时候我们需要扩展, 。


PHP从语言本质来说的确是一门不错的语言,如果灵活的运用,他不应该成为阻碍我们前进的瓶颈,反而是一把利器。

如果运用得当PHP真的是个不错的选择。 


现在进入正题:  

           通过C扩展PHP 

           实现函数 Usher_GetHostIP()  

           该函数的功能是获取系统IP 并且返回 无参数调用  


直接进入正题:

     首先进入PHP源代码 的 ext目录下 执行   ./ext_skel --extname=usher  这一步产生扩展架构  执行后如下图 

 产生了 usher扩展目录 

    



  进入 cd usher  ls 发现有如下文件  我这里是配置好的比大家多 无所谓 




运行 /usr/local/php/bin/phpize 根据你的路径选择phpize 这一步是autoconfig的利用 会产生上图中的 configure  


到了这一步OK 编译环境准备完毕 !!下面开始实现 我们的Usher_GetHostIP()


进入产生的usher 目录   编辑  usher.c  和php_usher.h 这两个文件是C的头文件和 源文件 我们需要在这里 扩展PHP 

我直接贴上代码 


打开php_usher.h  添加如下代码

  1. PHP_FUNCTION(Usher_GetHostIP); //注意这里 这是我们自己添加的  扩展函数  
结果如下

  1. #ifndef PHP_USHER_H  
  2. #define PHP_USHER_H  
  3.   
  4. extern zend_module_entry usher_module_entry;  
  5. #define phpext_usher_ptr &usher_module_entry  
  6.   
  7. #define PHP_USHER_VERSION "0.1.0" /* Replace with version number for your extension */  
  8.   
  9. #ifdef PHP_WIN32  
  10. #   define PHP_USHER_API __declspec(dllexport)  
  11. #elif defined(__GNUC__) && __GNUC__ >= 4  
  12. #   define PHP_USHER_API __attribute__ ((visibility("default")))  
  13. #else  
  14. #   define PHP_USHER_API  
  15. #endif  
  16.   
  17. #ifdef ZTS  
  18. #include "TSRM.h"  
  19. #endif  
  20.   
  21. PHP_MINIT_FUNCTION(usher);  
  22. PHP_MSHUTDOWN_FUNCTION(usher);  
  23. PHP_RINIT_FUNCTION(usher);  
  24. PHP_RSHUTDOWN_FUNCTION(usher);  
  25. PHP_MINFO_FUNCTION(usher);  
  26.   
  27. PHP_FUNCTION(confirm_usher_compiled);   /* For testing, remove later. */  
  28. PHP_FUNCTION(Usher_GetHostIP); //注意这里 这是我们自己添加的  扩展函数  
  29.   
  30. /*  
  31.     Declare any global variables you may need between the BEGIN 
  32.     and END macros here:      
  33.  
  34. ZEND_BEGIN_MODULE_GLOBALS(usher) 
  35.     long  global_value; 
  36.     char *global_string; 
  37. ZEND_END_MODULE_GLOBALS(usher) 
  38. */  
  39. #ifdef ZTS  
  40. #define USHER_G(v) TSRMG(usher_globals_id, zend_usher_globals *, v)  
  41. #else  
  42. #define USHER_G(v) (usher_globals.v)  
  43. #endif  
  44.   
  45. #endif  /* PHP_USHER_H */  


打开usher.c添加如下代码

  1. #include <netdb.h>    //我们添加的扩展代码  
  2. #include <sys/socket.h>  
  3. #include <netinet/in.h>  
  4. #include <arpa/inet.h>  

  1. //Usher_GetHostIP实现  
  2. PHP_FUNCTION(Usher_GetHostIP)  
  3. {  
  4.     char *arg = NULL;  
  5.     int arg_len, len;  
  6.     char *strg;  
  7.     struct hostent *he;  
  8.     char hostname[20] = {0};  
  9.     gethostname(hostname,sizeof(hostname));  
  10.     he = gethostbyname(hostname);  
  11.   
  12.     if (ZEND_NUM_ARGS() !=0) {  
  13.             RETURN_STRINGL("Prm error",strlen("Prm error") , 0);  
  14.             return;  
  15.     }  
  16.     len = spprintf(&strg, 0, "%s:%s""usher", inet_ntoa(*(struct in_addr*)(he->h_addr)));  
  17.     RETURN_STRINGL(strg, len, 0);  
  18. }  
最终如下 usher.c如下

  1. #ifdef HAVE_CONFIG_H  
  2. #include "config.h"  
  3. #endif  
  4.   
  5. #include "main/php.h"  
  6. #include "main/php_ini.h"  
  7. #include "ext/standard/info.h"  
  8. #include "php_usher.h"  
  9. #include <netdb.h>    //我们添加的扩展代码  
  10. #include <sys/socket.h>  
  11. #include <netinet/in.h>  
  12. #include <arpa/inet.h>  
  13.   
  14. static int le_usher;  
  15.   
  16. const zend_function_entry usher_functions[] =  
  17. {  
  18.         PHP_FE(Usher_GetHostIP, NULL)       /* For testing, remove later. */  
  19.     PHP_FE(confirm_usher_compiled,  NULL)       /* For testing, remove later. */  
  20.     PHP_FE_END  /* Must be the last line in usher_functions[] */  
  21. };  
  22. /* }}} */  
  23.   
  24. /* {{{ usher_module_entry 
  25.  */  
  26. zend_module_entry usher_module_entry = {  
  27. #if ZEND_MODULE_API_NO >= 20010901  
  28.     STANDARD_MODULE_HEADER,  
  29. #endif  
  30.     "usher",  
  31.     usher_functions,  
  32.     PHP_MINIT(usher),  
  33.     PHP_MSHUTDOWN(usher),  
  34.     PHP_RINIT(usher),       /* Replace with NULL if there's nothing to do at request start */  
  35.     PHP_RSHUTDOWN(usher),   /* Replace with NULL if there's nothing to do at request end */  
  36.     PHP_MINFO(usher),  
  37. #if ZEND_MODULE_API_NO >= 20010901  
  38.     PHP_USHER_VERSION,  
  39. #endif  
  40.     STANDARD_MODULE_PROPERTIES  
  41. };  
  42. /* }}} */  
  43.   
  44. #ifdef COMPILE_DL_USHER  
  45. ZEND_GET_MODULE(usher)  
  46. #endif  
  47. PHP_MINIT_FUNCTION(usher)  
  48. {  
  49.     return SUCCESS;  
  50. }  
  51. PHP_MSHUTDOWN_FUNCTION(usher)  
  52. {  
  53.   
  54.     return SUCCESS;  
  55. }  
  56.   
  57. PHP_RINIT_FUNCTION(usher)  
  58. {  
  59.     return SUCCESS;  
  60. }  
  61.   
  62. PHP_RSHUTDOWN_FUNCTION(usher)  
  63. {  
  64.     return SUCCESS;  
  65. }  
  66.   
  67. PHP_MINFO_FUNCTION(usher)  
  68. {  
  69.     php_info_print_table_start();  
  70.     php_info_print_table_header(2, "usher support""enabled");  
  71.     php_info_print_table_end();  
  72.   
  73. }  
  74. //Usher_GetHostIP实现  
  75. PHP_FUNCTION(Usher_GetHostIP)  
  76. {  
  77.     char *arg = NULL;  
  78.     int arg_len, len;  
  79.     char *strg;  
  80.     struct hostent *he;  
  81.     char hostname[20] = {0};  
  82.     gethostname(hostname,sizeof(hostname));  
  83.     he = gethostbyname(hostname);  
  84.   
  85.     if (ZEND_NUM_ARGS() !=0) {  
  86.             RETURN_STRINGL("Prm error",strlen("Prm error") , 0);  
  87.             return;  
  88.     }  
  89.     len = spprintf(&strg, 0, "%s:%s""usher", inet_ntoa(*(struct in_addr*)(he->h_addr)));  
  90.     RETURN_STRINGL(strg, len, 0);  
  91. }  
  92. PHP_FUNCTION(confirm_usher_compiled)  
  93. {  
  94.     char *arg = NULL;  
  95.     int arg_len, len;  
  96.     char *strg;  
  97.   
  98.     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {  
  99.         return;  
  100.     }  
  101.   
  102.     len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.""usher", arg);  
  103.     RETURN_STRINGL(strg, len, 0);  
  104. }  

代码 修改完毕 开始编译 

修改 usher目录下的config.m4 去掉下图所示的两行前的 dnl    (PHP_ARG_WITH 、 [ --with-usher 这两行的dnl)


直接 ./make   在usher目录下  如下图结果 就证明编译成功    


make install 安装   具体安装到哪里 看php的设置  我这里默认安装到  


生成了usher.so 



好了 扩展编完了 加载到 php 试试  启动 fpm 进入 phpinfo查看 我们的扩展加载了没   

看吧 usher support  enabled!!!!!

 

我们到PHP中编写代码测试吧 :

  1. echo Usher_GetHostIP();  

结果如下  我们成功的扩展了PHP 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值