PHP插件开发&&测试

  1.系统环境   

内核2.6.32,系统为CentOS6.2的64为系统,ip地址为192.168.29.155。如下:

1
2
3
4
5
6
7
[root@Michael ~]# uname -r
2.6.32-220.el6.x86_64
[root@Michael ~]# cat /etc/redhat-release
CentOS release 6.2 (Final)
[root@Michael ~]# ifconfig | grep addr: |awk '{print $2}' |awk -F: '{print $2}'
192.168.29.155
127.0.0.1

2.编译安装PHP   

编译安装php。   php-5.5.0.tar.bz2

1
2
3
4
5
6
7
8
9
10
[root@Michael ~]#  tar xf php-5.5.0.tar.bz2
[root@Michael ~]# cd  php-5.5.0
[root@Michael php-5.5.0]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fastcgi --disable-debug --disable-ipv6 --with-iconv-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap

[root@Michael php-5.5.0]#  make ||  make ZEND_EXTRA_LIBS='-liconv'
[root@Michael php-5.5.0]# make install


3.自定义扩展开发  
Document:
[root@Michael php-5.5.0]# more  README.EXT_SKEL
[root@Michael php-5.5.0]# wget http://www.php.net/manual/zh/internals2.ze1.zendapi.php
.
[ root@ Michael   michael]#cd ext
[ root@ Michael   ext]#. / ext_skel  -- extname =michael
[ root@ Michael   ext]# vi michael/config.m4
dnl Otherwise use enable:
dn1 PHP_ARG_ENABLE(michael, whether to enable michael support,
dn1 Make sure that the comment is aligned:
dn1 [ --enable-michael Enable michael support])
修改后:
dnl Otherwise use enable:
PHP_ARG_ENABLE(michael, whether to enable michael support,
Make sure that the comment is aligned:
[ --enable-michael Enable michael support])


[root@Michael michael]#vi michael.c

【往ZNED引擎注册新方法】
------------------------ ------------------------ ------------------------ ------------------------ -----------------
const zend_function_entry michael_functions[] = {
        PHP_FE(confirm_michael_compiled, NULL) /* For testing, remove later. */
        PHP_FE_END /* Must be the last line in michael_functions[] */
};

修改后
const zend_function_entry michael_functions[] = {
        PHP_FE(confirm_michael_compiled, NULL) /* For testing, remove later. */
        PHP_FE(xuchao, NULL) /* For testing, remove later. */
        PHP_FE(test, NULL) /* For testing, remove later. */
        PHP_FE_END /* Must be the last line in michael_functions[] */
};
---------------------------------- ------------------------ ------------------------ ----------- --- -----------------
【输出到PHPINFO】
PHP_MINFO_FUNCTION(michael)
{
        php_info_print_table_start();
        php_info_print_table_header(2, "michael support", "enabled");
        php_info_print_table_end();
        /* Remove comments if you have entries in php.ini
        DISPLAY_INI_ENTRIES();
        */
}
修改后
PHP_MINFO_FUNCTION(michael)
{
        php_info_print_table_start();
        php_info_print_table_header(2, "michael support", "enabled");
        php_info_print_table_row(2, "Michael Module", "This is frist php-extent!!!");
        php_info_print_table_row(2, "version", "Michael-1.0");
        php_info_print_table_end();
        /* Remove comments if you have entries in php.ini
        DISPLAY_INI_ENTRIES();
        */
}

------------------------ ------------------------ ------------------------ ------------------------ -----------------
【增加注册的方法】

PHP_FUNCTION(xuchao)
{
        char *arg = NULL;
        int arg_len, len;
        char *strg;
        php_printf("Hello World!\n");
        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.", "michael", arg);
        RETURN_STRINGL(strg, len, 0);
}
PHP_FUNCTION(test)
{
        char *arg = NULL;
        int arg_len, len;
        double max_len = 900000000;
        char *strg;
        double id;
        double cont=0;
        php_printf("Start:\n");
        for(id = 0;id < max_len;id++){
                cont = cont + max_len;
        }
        php_printf("END:%f",cont);
        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.", "michael", arg);
        RETURN_STRINGL(strg, len, 0);
}

------------------------ ------------------------ ------------------------ ------------------------ -----------------

[root@Michael michael]#vi php_michael.h
【修改.h头文件】
PHP_FUNCTION(confirm_michael_compiled); /* For testing, remove later. */ 
改为:
PHP_FUNCTION(confirm_michael_compiled); /* For testing, remove later. */
PHP_FUNCTION(xuchao); /* For testing, remove later. */
PHP_FUNCTION(test); /* For testing, remove later. */
-----------------------------------------------------------------------------------------------------------------

[root@Michael michael]#/usr/local/php/bin/phpize
[root@Michael michael]#./configure
[root@Michael michael]#make
[root@Michael michael]#make install


OR

[root@Michael php-5.5.0]#./buildconf --force
[root@Michael php-5.5.0]#./configure --help | grep michael
--enable-michael           Enable michael support
[root@Michael php-5.5.0]#./configure --enable-michael
[root@Michael php-5.5.0]#make
[root@Michael php-5.5.0]#make install
[root@Michael  php-5.5.0]# /usr/local/php/bin/php -m | grep michael 
michael

[root@Michael html] vi phpinfo.php
<?php
phpinfo();
?>
[root@Michael html] service php-fpm restart

以上步骤会遇到各种奇葩问题,Notice:干净系统级版本对应上, 如果为空,把相应的michael.so 注册到php.ini.
 PHP插件开发&&测试_第1张图片




4.扩展测试篇 
[root@Michael html]# more test.php
<?php
        $id;
        $max_len = 900000000;
        $cont = 0;
        for($id=0;$id < $max_len; $id++){
                $cont = $cont + $max_len;
        }
        echo $cont;
?>

[root@Michael php-5.5.0]#more index.php
<?php
        test();
?>
PHP插件开发&&测试_第2张图片
密集型过亿级别计算,差距竟然这么大。




你可能感兴趣的:(PHP插件开发&&测试)