1 创建函数定义文件
#mkdir /home/phpext
#vi mydefined.skelstring get_text(string str)
2 根据README所提供的信息创建预定义文件和扩展的开发框架包
进入到PHP源码包,即php-5.*/ext/内 运行下面代码 将会生成hello文件夹
# ./ext_skel --extname=hello --proto=/home/phpext/mydefined.skel
3 进入hello文件夹 修改hello文件内,config.m4、php_hello.h、hello.c三个文件
[[email protected] hello]#vi config.m4
dnl PHP_ARG_WITH(hello, forhello support,
dnl Make sure that the commentisaligned:
dnl [--with-hello Include hello support])
dnl Otherwise use enable:
PHP_ARG_ENABLE(hello, whether to enable hello support,
Make sure that the commentisaligned:
[--enable-hello Enable hello support])
注释掉PHP_ARG_WITH或PHP_ARG_ENABLE(根据实际情况二选一,第一种是指扩展需第三方库支持,我这去掉第二个的注释dnl)
[[email protected] hello]#vi php_hello.h
/*PHP_FUNCTION(confirm_hello_compiled);*/ /*For testing, remove later.*/PHP_FUNCTION(get_text);
注释掉默认声明的PHP函数 confirm_hello_compiled
[[email protected] hello]# vi hello.c
const zend_function_entry hello_functions[] ={/*PHP_FE(confirm_hello_compiled, NULL)*/ /*For testing, remove later.*/PHP_FE(get_text, NULL)
PHP_FE_END/*Must be the last line in hello_functions[]*/};/*PHP_FUNCTION(confirm_hello_compiled)*/PHP_FUNCTION(get_text)
{char *arg =NULL;intarg_len, len;char *strg;if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) ==FAILURE) {return;
}
len= spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "hello", arg);
RETURN_STRINGL(strg, len,0);
}
注释掉默认声明的PHP函数 confirm_hello_compiled 并鸠占鹊巢 用get_text 代替confirm_hello_compiled (主要使用里面现成的打印内容;可以自己写要执行的内容实现getext函数 需熟悉C)
4 配置、编译、安装phpext
[[email protected] hello]# /usr/local/php/bin/config
[[email protected] hello]#make&& make install
如果一切正常,将在对应的文件夹内将多出一个叫hello.so的文件,具体地址编译后会返回 在php.ini中添加扩展并重启 如
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/hello.so
查看是否安装成功
#php -m|grep hello
hello
[[email protected] hello]# php -r "echo get_text(‘hello‘);"Congratulations! You have successfully modified ext/hello/config.m4. Module hello is now compiled into PHP.
下一步学习C 任重道远...
原文:http://www.cnblogs.com/wangxusummer/p/4995478.html