+ -
当前位置:首页 → 问答吧 → 用C来扩展我们的PHP

用C来扩展我们的PHP

时间:2007-12-25

来源:互联网

用C语言写PHP扩展的步骤
2007-12-26 20:56

用C语言,php的扩展的书写格式(ZEND API)写PHP扩展的步骤:



到PHP的安装目录下

[root@test1 ext]# cd /root/php/php5.2/ext

[root@test1 ext]# ./ext_skel --extname=cltest


修改 配置文件config.m4

[root@test1 ext]# vi cltest/config.m4

删除 3 个 dnl

dnl PHP_ARG_WITH(my_module, for my_module support,

dnl Make sure that the comment is aligned:

dnl [ --with-my_module Include my_module support])


或者删除 下边这3个 dnl

dnl PHP_ARG_ENABLE(my_module, whether to enable my_module support,

dnl Make sure that the comment is aligned:

dnl [ --enable-my_module Enable my_module support])

修改完毕。


在my_module.c扩展模块的主程序文件中加PHP的函数定义

主程序中描述了php扩展模块的声明,模块中含有多少个函数,各个函数的作用,在phpinfo函数中显示什么内容,模块初始化做些什么,结束做些什么都会在这个文件里进行描述。我们在上面只是添加了一个函数say_hello,并且描述了say_hello函数的具体内容

[root@test1 ext]# Vi cltest.c

function_entry my_module_functions[] = {
PHP_FE(say_hello, NULL) /* ?添加这一行代码 注册函数say_hello() */

PHP_FE(confirm_my_module_compiled, NULL) /* For testing, remove later. */

{NULL, NULL, NULL} /* Must be the last line in my_module_functions[] */

};

在文件的最后添加下列代码 函数程序主体

PHP_FUNCTION(say_hello)
{
RETURN_STRINGL("hello world",100,1);
}

修改完毕。



在my_module.h扩展模块的头文件中加PHP启动时要注册的函数

在对应的头文件中声明了say_hello这个函数,从而完成了我们预期的功能。

[root@test1 ext]# vi php_cltest.h

在文件中PHP_FUNCTION(confirm_my_module_compiled);一行前面添加下面的代码 函数定义

PHP_FUNCTION(say_hello);

保存文件退出,修改完毕。

找到这个执行文件phpize ,在cltest目录下执行命令,用于加载动态链接库

[root@test1 cltest]# /usr/local/php/bin/phpize

注意apxs的版本 ,使用 --with-apxs 或者 --with-apxs2 命令

[root@test1 cltest]# find / -name apxs -print


执行编译命令

[root@test1 cltest]# ./configure --enable-cltest --with-apxs=/usr/local/apache/bin/apxs --with-php-config=/usr/local/php/bin/php-config


//下边的这部分是 想调用其他的C的动态链接库的 ,还没有测试成功。
php_test.so 复制到、/usr/lib/
#include "php_test.h"
直接使用php_test.so提供的接口函数strToupper()。

[root@test1 cltest]# ./configure --enable-cltest LDFLAGS=-lphp_test --with-apxs=/usr/local/apache/bin/apxs --with-php-config=/usr/local/php6/bin/php-config


在php的这个扩展库的Makefile里面的EXTRA_LIBS加上这个libtest.a

//上边的这部分是 想调用其他的C的动态链接库的 ,还没有测试成功。


[root@test1 cltest]# make


会在当前的目录下生成一个目录叫modules他的下面就放着你要的cltest.so文件

[root@test1 cltest]# cp modules/cltest.so /usr/local/php/include/php/ext/

这里需要你先设置你的php的扩展目录的 在php.ini里面查找“extension_dir”


找到php的执行文件 /usr/local/php/bin/php

[root@test1 ext]# find / -name php -print

[root@test1 ext]# ./php -f /root/php/php5.2/ext/cltest/cltest.php // 测试是否 已经加载成功

在php.ini文件中打开这个扩展/usr/local/php/lib/php.ini

extension=jinzhesheng_module.so

然后 重新起动apache

sudo /usr/sbin/apachectl stop

sudo /usr/sbin/apachectl start

[root@test1 ext]# find / -name apachectl -print
[root@test1 ext]# /apachectlPATH/apachectl stop
[root@test1 ext]# /apachectlPATH/apachectl start

用phpinfo来察看一下

以后只要修改cltest.c文件,在执行make命令,重新启动APACHE 就可以更新 动态链接库

例如:增加函数chenlong


[root@test1 cltest]# vi ext/cltest/cltest.c

增加:PHP_FE(chenlong, NULL)

PHP_FUNCTION(chenlong)
{
//带参数

zval **yourname;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &yourname) == FAILURE)
{
WRONG_PARAM_COUNT;
}
zend_printf("hello world, %s\n", Z_STRVAL_PP(yourname));
}

[root@test1 cltest]# vi php_cltest.h

增加:PHP_FUNCTION(chenlong);


[root@test1 cltest]# make

[root@test1 cltest]# cp modules/ctest.so /usr/local/php/include/php/ext/

[root@test1 ext]# /apachectlPATH/apachectl stop

[root@test1 ext]# /apachectlPATH/apachectl start

测试:

Say_hello();
chenlong("DragonChen");
?>

Makefile文件中:

EXTRA_CFLAGS=-I/usr/local/bind/include
EXTRA_LDFLAGS=-L/usr/local/bind/lib
EXTRA_LIBS=-lbind

cp modules/ctest.so /usr/local/php/include/php/ext/
/usr/sbin/apachectl stop
/usr/sbin/apachectl start




[ 本帖最后由 逆雪寒 于 2007-12-29 14:08 编辑 ]

作者: 逆雪寒   发布时间: 2007-12-24

这方面的资料确实少,如果真是楼主一字一字敲出来的,佩服你的贡献精神!
前两天还见你抱怨phper工资低,不如java,如果以你的水平还这样抱怨,我看大多数学php的人对php该就此止步了 。真的没钱图了

作者: flynetcn   发布时间: 2007-12-25

楼主的头像... ...

作者: flynetcn   发布时间: 2007-12-25

上面文章转帖的。

学PHP是为了兴趣。如果为了挣很多很多钱。
希望大家别学开发PHP。
学开发人力资源去

作者: 逆雪寒   发布时间: 2007-12-25

版主无处不在啊!

作者: hongfu1   发布时间: 2007-12-25

引用:
原帖由 flynetcn 于 2007-12-25 11:32 发表
楼主的头像... ...

作者: luzhou   发布时间: 2007-12-25

人才

作者: hxunphp   发布时间: 2007-12-29

楼主的头像不错,有创意...

作者: leejianjun   发布时间: 2007-12-31

m都看不到~

作者: qingis   发布时间: 2007-12-31

转载的东西就不应该搞来原创吧

作者: davy   发布时间: 2007-12-31

对   啊    !!!!!!!!!!!!!!!!!!!!!!!!!!!!

这可是 '原创'版啊,晕!~

作者: flashjay   发布时间: 2008-01-01

顶下

作者: blankyao   发布时间: 2008-01-02

这篇文章相当好,就是有点不明白,继续努力啊。

作者: netlawe   发布时间: 2008-01-04

哥们“原创”挺多了

作者: pizzro   发布时间: 2008-01-16

三.  调用第三方C/C++库

在PHP扩展模块中调用第三方的C/C++库,与普通的C/C++程序调用C/C++库一样,只要把库文件的位置放对就可以了。下面举例说明:

1. 调用动态库a.so

需要的文件,a.so,a.h(a.h主要做数据类型声明和入口函数声明),都放在 test_module目录下。

调用a.so:

1)   在test_module.c的开头添加a.h:

#include "php.h

#include "php_ini.h"
#include "a.h"
添加了头文件后,可以在test_module.c的函数中调用a.so的函数接口。

2)    重新编译链接,链接时加入a.so:

# cd phpsrc/ext
#cc -fpic -DCOMPILE_DL_TEST_MODULE=1 -I/usr/local/include -I. -I../main-I.. -I../TSRM -I../Zend -c -o test_module/test_module.otest_module/test_module.c

执行完之后会在 目录下生成一个test_module.o文件,接下来连接:

# cc -shared -L/usr/local/lib -rdynamic -o test_module/test_module.so test_module/test_module.o test_module/a.so

[如果a.so内部实现是C++,链接时还应该加入参数 �Clstdc++,即:

# cc �Cshared �Clstdc++ -L/usr/local/lib -rdynamic -o test_module/test_module.so test_module/test_module.o test_module/a.so

]

3)    测试test_module.so时,把test_module.so和a.so都拷贝到phpbin/lib/php/extensions/下。

2. 调用静态库a.a

需要的文件,a.so,a.h(a.h主要做数据类型声明和入口函数声明),都放在 test_module目录下。

调用a.a:

1)     和2)都与调用a.so相同。

3)   测试test_module.so时,把test_module.so拷贝到phpbin/lib/php/extensions/下,a.a不需要。

作者: 逆雪寒   发布时间: 2008-03-22