您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

开发的第一个PHP扩展

2024/4/13 18:05:01发布22次查看
下载php源码php-5.4.23.tar.gz,解压,进入/home/hubo/php-5.4.23/ext/扩展目录 wget http://cn2.php.net/get/php-5.4.23.tar.gz/from/this/mirror tar -xzvf php-5.4.23.tar.gz cd php-5.4.23/ext/ 在ext目录中新建config.m4文件 php_arg_enable(heiyoubo,
下载php源码php-5.4.23.tar.gz,解压,进入/home/hubo/php-5.4.23/ext/扩展目录
wget http://cn2.php.net/get/php-5.4.23.tar.gz/from/this/mirror
tar -xzvf php-5.4.23.tar.gz
cd php-5.4.23/ext/
在ext目录中新建config.m4文件
php_arg_enable(heiyoubo, [whether to enable the heiyoubo extension], [ enable-heiyoubo enable heiyoubo extension support])if test $php_heiyoubo != no; then php_subst(heiyoubo_shared_libadd) php_new_extension(heiyoubo, heiyoubo.c, $ext_shared)fi
在ext目录中新建heiyoubo.c文件
#ifdef have_config_h#include config.h#endif//加载php头文件#include php.h#define phpext_heiyoubo_ptr &heiyoubo_module_entryzend_function(heiyoubo_hello){ php_printf(hello world heiyoubo!\n);}static zend_function_entry heiyoubo_functions[] = { zend_fe(heiyoubo_hello, null) { null, null, null }};//module entryzend_module_entry heiyoubo_module_entry = { #if zend_module_api_no >= 20010901 standard_module_header,#endif heiyoubo, //这个地方是扩展名称,往往我们会在这个地方使用一个宏。 heiyoubo_functions, /* functions */ null, /* minit */ null, /* mshutdown */ null, /* rinit */ null, /* rshutdown */ null, /* minfo */#if zend_module_api_no >= 20010901 2.1, //这个地方是我们扩展的版本#endif standard_module_properties};#ifdef compile_dl_heiyoubozend_get_module(heiyoubo)#endif
运行phpize,准备 php 扩展库的编译环境
[hubo@test15169x ~/php-5.4.23/ext]$ /usr/local/php/bin/phpize
configuring for:
php api version: 20100412
zend module api no: 20100525
zend extension api no: 220100525
configure的时候要开启heiyoubo扩展,并且指定php-config的目录,获取所安装的 php 配置的信息
[hubo@test15169x ~/php-5.4.23/ext]$ ./configure --enable-heiyoubo --with-php-config=/usr/local/php/bin/php-config
[hubo@test15169x ~/php-5.4.23/ext]$ make
[hubo@test15169x ~/php-5.4.23/ext]$ make test
heiyoubo.so扩展已经生成到module目录
[hubo@test15169x ~/php-5.4.23/ext]$ ll modules/*
-rw-rw-r-- 1 hubo hubo 799 01-09 16:53 modules/heiyoubo.la
-rwxrwxr-x 1 hubo hubo 26k 01-09 16:53 modules/heiyoubo.so
将heiyoubo.so文件拷贝到php的扩展目录
[hubo@test15169x ~/php-5.4.23/ext]$ php -ini | grep extension_dir
extension_dir => /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525 => /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525
[hubo@test15169x ~/php-5.4.23/ext]$ cp  modules/heiyoubo.so /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
将heiyoubo.so加到php的扩展文件/usr/local/etc/cgi/php.ini中  extension = heiyoubo.so
[hubo@test15169x ~/php-5.4.23/ext]$ php --ini
configuration file (php.ini) path: /usr/local/etc/cgi
loaded configuration file: /usr/local/etc/cgi/php.ini
[hubo@test15169x ~/php-5.4.23/ext]$ php -r 'var_dump(get_loaded_extensions());' | grep heiyoubo
string(8) heiyoubo
验证安装成功。执行c扩展中函数heiyoubo_hello();执行成功
[hubo@test15169x ~/php-5.4.23/ext]$ php -r 'heiyoubo_hello();'
hello world heiyoubo!
参考链接:
http://www.php.net/manual/zh/internals2.buildsys.configunix.php
https://github.com/walu/phpbook/blob/master/5.1.md           《extending and embedding php》中文版翻译      php扩展开发及内核应用
http://www.laruence.com/2009/04/28/719.html
http://www.php-internals.com/
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product