码迷,mamicode.com
首页 > Web开发 > 详细

用ext_skel,实现一个PHP扩展,添加到PHP并调用

时间:2015-11-25 18:58:28      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

1 创建函数定义文件

#mkdir /home/phpext
#vi mydefined.skel

string get_text(string str)

根据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三个文件

[root@localhost hello]#vi config.m4

dnl PHP_ARG_WITH(hello, for hello support,
dnl Make sure that the comment is aligned:
dnl [  --with-hello             Include hello support])

dnl Otherwise use enable:

PHP_ARG_ENABLE(hello, whether to enable hello support,
Make sure that the comment is aligned:
[  --enable-hello           Enable hello support])

注释掉PHP_ARG_WITH或PHP_ARG_ENABLE(根据实际情况二选一,第一种是指扩展需第三方库支持,我这去掉第二个的注释dnl)

[root@localhost hello]#vi php_hello.h

 /*PHP_FUNCTION(confirm_hello_compiled);*/   /* For testing, remove later. */
 PHP_FUNCTION(get_text);

注释掉默认声明的PHP函数 confirm_hello_compiled

[root@localhost 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;
    int arg_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)

配置、编译、安装phpext

[root@localhost hello]# /usr/local/php/bin/phpize
[root@localhost hello]#./configure --with-php-config=/usr/local/php/bin/php-config
[root@localhost 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
[root@localhost hello]#  php -r "echo get_text(‘hello‘);"
Congratulations! You have successfully modified ext/hello/config.m4. Module hello is now compiled into PHP.

 下一步学习C 任重道远...

用ext_skel,实现一个PHP扩展,添加到PHP并调用

标签:

原文地址:http://www.cnblogs.com/wangxusummer/p/4995478.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!