php扩展开发
1 下载源代码2 进入ext目录
3 ./ext_skel --extname=shb
shb为模块名
4 编辑 php_shb.h(默认头文件都是带php前缀)
加入
PHP_FUNCTION(shb_say_hello);
5 编辑shb.c
5.1 修改代码
原始代码:
const zend_function_entry shb_functions[] = {
PHP_FE(confirm_shb_compiled, NULL) /* For testing, remove later. */
PHP_FE_END /* Must be the last line in shb_functions[] */
};
/* }}} */
修改后:
const zend_function_entry shb_functions[] = {
PHP_FE(shb_say_hello, NULL)
PHP_FE(confirm_shb_compiled, NULL) /* For testing, remove later. */
PHP_FE_END /* Must be the last line in shb_functions[] */
};
/* }}} */
5.2 添加代码
PHP_FUNCTION(shb_say_hello)
{
char *arg = NULL;
int arg_len, len;
char *strg;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
return;
}
zend_printf("hello:%s", arg);
RETURN_STRINGL(arg, arg_len, 1);
}
6 phpzie
7 ./configure -with-php-config=/usr/local/bin/php-config
php-config的目录不一定和我一样,自己找一下。
8 make
9 sudo make install
Installing shared extensions: /usr/local/lib/php/extensions/no-debug-non-zts-20121212/
10 找到php.ini
加入
extension = /usr/local/lib/php/extensions/no-debug-non-zts-20121212/shb.so
11 重启apache,如果是nginx则需要重启php-cgi
12 php -m查看模块是否被加载
13 测试代码
<?php
$r = shb_say_hello("sunhaibing.com");
?>
输出
hello:sunhaibing.com
查看完整版本: php扩展开发
Tags: php