本文中需要用到上一篇文章中生成的调用WebService客户端程序(具体内容请参见 [url]http://commandos.blog.51cto.com/154976/130652[/url]),只不过需要编译成动态或静态链接库的形式。本文中,将WebService客户端程序中的main()改名为testPhpModule(),并将打印到控制台的字符串返回,编译完成后生成文件名为libTest.so。
一、环境准备
将生成的libTest.so文件拷贝到/usr/lib目录下,并执行命令/sbin/ldconfig
准备PHP的源代码文件,解压缩。
二、开发PHP Module
首先进入PHP源代码目录中的ext目录,执行如下命令:
# ./ext_skel --extname=自定义模块名
执行完成后,会生成以“自定义模块名”命名的文件夹,进入这个文件夹后编辑config.m4文件
找到如下代码:
dnl PHP_ARG_WITH(Test, for Test support,    
dnl Make sure that the comment is aligned:    
dnl [    --with-Test                         Include Test support])
或者
dnl PHP_ARG_ENABLE(Test, whether to enable Test support,    
dnl Make sure that the comment is aligned:    
dnl [    --enable-Test                     Enable Test support])
去掉每行前面的dnl
如果是想通过动态引用,建议使用--enable-Test
三、编写调用函数
编辑Test.c文件,找到如下代码:
zend_function_entry Test_functions[] = {    
                PHP_FE(confirm_Test_compiled,        NULL)                     /* For testing, remove later. */    
                PHP_FE(test, NULL)    /* 增加这一行,其中test代表在PHP文件中的函数调用名 */    
                {NULL, NULL, NULL}            /* Must be the last line in Test_functions[] */    
};
在Test.c文件的最后,增加代码:
PHP_FUNCTION(test)    
{    
        RETURN_STRING(testPhpModule());    
}
进行编译:
$ ./configure –with-php-config=${PHP_HOME}/bin/php-config
$ make LDFLAGS=-lTest
$ make test
四、测试Module
将编译完成后的模块so文件拷贝到Apache目录下:
# cp modules/Test.so /usr/local/apache2/htdocs/
在/usr/local/apache2/htdocs/目录下编辑PHP文件
# vi test.php
文件中的代码如下:
<?php    
        dl(“Test.so”);    
        echo test();    
?>
启动Apache,访问test.php,成功的话页面上会显示和main()函数同样的输出结果。