[扩展] php调用动态链接库 .so

如题,在调用该so文件时候,还需要给传递参数,不知php能否调用

int dl(string library); 只能调用动态链接库

作者: cqf820   发布时间: 2006-07-21

只能调用php自己的扩展库,也就是符合php扩展规则的,而不是普通意义的。

作者: HonestQiao   发布时间: 2006-07-21

如果说是写一个动态扩展库,再给扩展库里面的接口传递参数,是否可行?
如果可行,小乔能否帮给点指导..

作者: cqf820   发布时间: 2006-07-24

你可以先写一个php的扩展库,这个对你外部的扩展库进行包装,就可以了。

作者: HonestQiao   发布时间: 2006-07-24

php的扩展库, 我在精华贴里没有找到。是否可以讲解稍微详细点,谢谢。

作者: cqf820   发布时间: 2006-07-24

如果我这里写成了扩展库,C那边xxxx.so的动态链接库不用修改吧。

作者: cqf820   发布时间: 2006-07-24

不用,你只要在你的php扩展库做好外部调用即可。

作者: HonestQiao   发布时间: 2006-07-24

xiaoqiao: 怎么写php扩展哭.....汗.
看了一天的zend api, 没搞明白...

作者: cqf820   发布时间: 2006-07-25

正参考http://dev.csdn.net/develop/article/12/12420.shtm这个在做扩展库,如遇到问题再向小乔请教。
谢谢

作者: cqf820   发布时间: 2006-07-25

./buildconf
在执行这的时候,出现
[root@Caiqf php-5.1.2]# ./buildconf
You should not run buildconf in a release package.
use buildconf --force to override this check.

不知道这是什么意思......,过程仿照楼上csdn的帖子

作者: cqf820   发布时间: 2006-07-25

终于找到一个比较好的文章(打下广告 ): http://www.toplee.com/blog/?p=56&pp=7

[root@Caiqf php-4.3.10]# ./buildconf
You should not run buildconf in a release package.
use buildconf --force to override this check.
[root@Caiqf php-4.3.10]# ./buildconf --force
Forcing buildconf
using default Zend directory
buildconf: checking installation...
buildconf: autoconf version 2.57 (ok)
buildconf: Your version of autoconf likely contains buggy cache code.
           Running cvsclean for you.
           To avoid this, install autoconf-2.13 and automake-1.5.
buildconf: libtool version 1.4.3 (ok)
rebuilding configure
rebuilding main/php_config.h.in
WARNING: Using auxiliary files such as `acconfig.h', `config.h.bot'
WARNING: and `config.h.top', to define templates for `config.h.in'
WARNING: is deprecated and discouraged.

WARNING: Using the third argument of `AC_DEFINE' and
WARNING: `AC_DEFINE_UNQUOTED' allows to define a template without
WARNING: `acconfig.h':

WARNING:   AC_DEFINE([NEED_MAIN], 1,
WARNING:             [Define if a function `main' is needed.])

WARNING: More sophisticated templates can also be produced, see the
WARNING: documentation.
如果遇到上面问题,请看该贴: http://www.squid-cache.org/mail- ... rs/200603/0969.html
autoconfxxxxx.tar.gz和automakexxxx.tar.gz自己找一下

希望能有帮助.

作者: cqf820   发布时间: 2006-08-04

这个问题 我遇到了 但是查看资料都是说理论的,在我解决这个问题,我一定要写出详细的开发、测试步骤,方便后人学习。。。

作者: dragonchen82   发布时间: 2007-05-15

用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

作者: dragonchen82   发布时间: 2007-05-15