您好,欢迎访问一九零五行业门户网

PHP扩展开发-LINUX环境的代码实例分享

linux环境下开发php扩展的步骤如下:
1、下载php源码,解压,我的解压目录是:/root/lamp/php-5.5.37
2、cd到/root/lamp/php-5.5.37/ext目录下,创建文件test_extension.def文件
int a(int x, int y)string b(string str, int n)
3、通过扩展框架生成器生成框架目录:
ext_skel –extname=test_extension –proto=test_extension.def
生成成功结果如下:
creating directory test_extension awk: /root/lamp/php-5.5.37/ext/skeleton/create_stubs:56: warning: escape sequence `\|' treated as plain `|' creating basic files: config.m4 config.w32 .svnignore test_extension.c php_test_extension.h credits experimental tests/001.phpt test_extension. php [done].to use your new extension, you will have to execute the following steps: 1. $ cd .. 2. $ vi ext/test_extension/config.m4 3. $ ./buildconf 4. $ ./configure --[with|enable]-test_extension 5. $ make 6. $ ./sapi/cli/php -f ext/test_extension/test_extension.php 7. $ vi ext/test_extension/test_extension.c 8. $ make repeat steps 3-6 until you are satisfied with ext/test_extension/config.m4 and step 6 confirms that your module is compiled into php. then, start writing code and repeat the last two steps as often as necessary.
4、切换到生成的框架目录下:cd test_extension
5、修改配置文件config.m4,去掉10、11、12行前面的dnl,如下
php_arg_with(test_extension, for test_extension support, make sure that the comment is aligned: [ --with-test_extension include test_extension support])
6、实现函数a和b的功能,vi test_extension.c,修改后函数a、b如下
php_function(a) { int argc = zend_num_args(); long x; long y; if (zend_parse_parameters(argc tsrmls_cc, "ll", &x, &y) == failure) { php_error(e_warning, "zend_parse_parameters failure!"); return; } return_long(x + y); } php_function(b) { char *str = null; int argc = zend_num_args(); int str_len; long n; char *result; char *ptr; int result_length; if (zend_parse_parameters(argc tsrmls_cc, "sl", &str, &str_len, &n) == failure) { php_error(e_warning, "zend_parse_parameters failure!"); return; } result_length = str_len * n; result = (char *) emalloc(result_length + 1); ptr = result; while (n--) { memcpy(ptr, str, str_len); ptr += str_len; } *ptr = '/0'; return_stringl(result, result_length, 0); }
7、test_extension目录下执行:/usr/local/bin/phpize
configuring for: php api version: 20121113zend module api no: 20121212zend extension api no: 220121212
8、配置:./configure –with-php-config=/usr/local/bin/php-config
9、编译:make
10、安装:make install
安装完成后/usr/local/lib/php/extensions/no-debug-zts-20121212/下会生成test_extension.so
11、修改php.in,加上:extension=test_extension.so
以上就是php扩展开发-linux环境的代码实例分享的详细内容。
其它类似信息

推荐信息