PHP扩展有2种,ZEND扩展,,再和PHP扩展,,ZEND扩展麻烦,,需要功力强大
PHP扩展简单点,生成.dll文件,动态加入。。。
首先,确定你电脑装上VC++环境,好编译扩展,注意配置好环境变量,在我的电脑,属性,环境变量PATH加上Common\MSDev98\Bin(绝对路径)
再次,准备一份PHP源代码+一份二进制php5ts.lib文件
OK,可以开始了:
把源码放在你的任意目录,复制skeleton文件夹命名为 sun, php_skeleton.h、skeleton.c、skeleton.dsp这三个文件,替换内容中所有extname为sun,EXTNAME为SUN
编辑php_skeleton.h文件(头文件)
在PHP_FUNCTION(confirm_sun_compiled);下面编写
PHP_FUNCTION(hello);
声明一个hello函数
编辑skeleton.c文件(主文件)
在PHP_FE(confirm_sun_compiled, NULL) 下面编写
PHP_FE(hello, NULL)
这是函数入口,下面该写函数主体了
找到PHP_FUNCTION(confirm_test_compiled)函数,该函数是测试函数,在该函数后面新写一个函数
PHP_FUNCTION(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;
}
len = spprintf(&strg, 0, "Hello,%s", arg);
RETURN_STRINGL(strg, len, 0);
}
运行cmd,进入sun
msdev sun.dsp /MAKE "sun - Win32 Release_TS"
如果没有错误,则在php_src目录下会生成一个Release_TS文件夹,里面就是编译好的php_sun.dll扩展
将其拷入运行环境中的php扩展目录ext
编辑php.ini添加extension=php_sun.dll,重启apache
在php文件中执行如下语句
echo hello('SUN SUN');
将输出
Hello,SUN SUN
OK,一个最简单的PHP扩展就出来了。。。