您的位置:首页 > 编程语言 > C语言/C++

利用php调用C语言hello.so库文件中的php扩展函数代码

2011-04-14 09:06 771 查看
某个功能被编译到so文件中,那么如何通过php来调用它?一个方法是写一个php模块(php extension),在php中调用该模块内的函数,再通过该模块来调用so中的函数。下面做一个简单的例子,使用的操作系统是Fedora Core 6。
某个功能被编译到so文件中,那么如何通过php来调用它?一个方法是写一个php模块(php extension),在php中调用该模块内的函数,再通过该模块来调用so中的函数。下面做一个简单的例子,使用的操作系统是Fedora Core 6。

首先做一个简单的so文件:

/**

* hello.c

* To compile, use following commands:

*   gcc -O -c -fPIC -o hello.o hello.c

*   gcc -shared -o libhello.so hello.o

*/

int hello_add(int a, int b)

{

return a + b;

}

然后将它编译成.so文件并放到系统中:

$ gcc -O -c -fPIC -o hello.o hello.c

$ gcc -shared -o libhello.so hello.o

$ su

# echo /usr/local/lib > /etc/ld.so.conf.d/local.conf

# cp libhello.so /usr/local/lib

# /sbin/ldconfig

写段小程序来验证其正确性:

/**

* hellotest.c

* To compile, use following commands:

*   gcc -o hellotest -lhello hellotest.c

*/

#include <stdio.h>

int main()

{

int a = 3, b = 4;

printf("%d + %d = %d

", a, b, hello_add(a,b));

return 0;

}

编译并执行:

$ gcc -o hellotest -lhello hellotest.c

$ ./hellotest

3 + 4 = 7

OK,下面我们来制作PHP模块。首先确保你安装了 php-devel 包,没有的话请自行从安装光盘上找。然后下载php源代码。我使用的是php-5.2.3.tar.gz,解压缩。

$ tar xzvf php-5.2.3.tar.gz

$ cd php-5.2.3/ext

然后通过下面的命令建立一个名为 hello 的模块。

$ ./ext_skel --extname=hello

执行该命令之后它会提示你应当用什么命令来编译模块,可惜那是将模块集成到php内部的编译方法。如果要编译成可动态加载的 php_hello.so,方法要更为简单。

$ cd hello

首先编辑 config.m4 文件,去掉第16行和第18行的注释(注释符号为 dnl 。)

16:  PHP_ARG_ENABLE(hello, whether to enable hello support,

17:  dnl Make sure that the comment is aligned:

18:  [  --enable-hello           Enable hello support])

然后执行 phpize 程序,生成configure脚本:

$ phpize 该程序在ubuntu的php5-dev包中

然后打开 php_hello.h,在 PHP_FUNCTION(confirm_hello_compiled); 之下加入函数声明:

PHP_FUNCTION(confirm_hello_compiled);   /* For testing, remove later. */

PHP_FUNCTION(hello_add);

打开 hello.c,在 PHP_FE(confirm_hello_compiled, NULL) 下方加入以下内容。

zend_function_entry hello_functions[] = {

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

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

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

};

然后在 hello.c 的最末尾书写hello_add函数的内容:

PHP_FUNCTION(hello_add)

{

long int a, b;

long int result;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &a, &b) == FAILURE) {

return;

}

result = hello_add(a, b);

RETURN_LONG(result);

}

保存退出,编译并安装:

$ ./configure

$ make LDFLAGS=-lhello

luther@gliethttp:~/php-5.2.13/ext/hello$ sudo make install

Installing shared extensions:     /usr/lib/php5/20060613+lfs/
$ su

# cp modules/hello.so /usr/lib/php/modules

luther@gliethttp:~$ sudo vim /etc/php5/apache2/php.ini

;enable_dl = Off

;允许dl()动态加载so扩展功能

enable_dl = On

luther@gliethttp:~$ sudo service apache2 reload

然后在 /var/www/html 下建立一个 hello.php 文件,内容如下:

<?php

dl("hello.so");

echo hello_add(3, 4);

?>

然后在浏览器中打开hello.php文件,如果显示7,则说明函数调用成功了。

另外可以在apache重启的时候让我们的so库直接动态编译进php5,就像linux的insmod hello.ko模块一样,不用dl加载也不用重新编译php,就可以直接使用so的函数了,步骤如下:
luther@gliethttp:~$ sudo vim /etc/php5/apache2/php.ini

enable_dl = Off

extension=hello.so

luther@gliethttp:~$ sudo service apache2 restart 不能reload而必须restart
apache,这样so就像insmod hello.ko一样被融到了php5内核,然后代码就可以忽略掉dl("hello.so");了,[注意,这种方式只适合hello.so库内所有功能代码已经全部调试ok,如果还处在调试期间,那么需要采用上面的dl强制加载的方式]
代码如下:
<?php

echo hello_add(3, 4);

?>

,但是该功能不太适合调试,因为每次修改hello.so中代码的话,都需要让service apache

restart重启才能让php5内核再次加载新的hello.so扩展.

可以这样定义hello.so的实现,这样每次执行.php网页,都会在/var/www/下建立一个文件夹,所以php扩展实现了

Browser网页浏览端和Server服务端apache之间没有了远端和本地之分,Browser可以直接执行

Server服务端的任何php扩展出来的so实现的函数,包括so扩展中实现的删除Server端的本地文件和打开/dev/下面的设备,

当然对php扩展so的调用都是由apache完成的,所以操作用户都是www-data,

因此不论删除apache端的文件还是打开
apache端的
/dev/设备都要使用户www-data具有相应操作权限[luther.gliethttp]

luther@gliethttp:~$ chmod a+w /var/www 允许www-data用户向/var/www目录写

#include <stdio.h>

#include <sys/stat.h>

#include <sys/types.h>

#include <fcntl.h>

#include <stdlib.h>

int hello_add(int a, int b)

{

int fd;

char file[256];

fd = open("/dev/null", O_APPEND);

sprintf(file, "luther.gliethttp =%d==", fd);

mkdir(file, 0700);

return a + b;

}

luther@gliethttp:~$ ll /var/www/

total 20K

-rw-r--r-- 1 luther   luther    447 2010-03-28 14:06 index.php

drwx------ 2 www-data www-data 4.0K 2010-03-28 14:35 luther.gliethttp =12==

drwx------ 2 www-data www-data 4.0K 2010-03-28 14:35 luther.gliethttp =13==

drwx------ 2 www-data www-data 4.0K 2010-03-28 14:35 luther.gliethttp =15==

drwx------ 2 www-data www-data 4.0K 2010-03-28 14:35 luther.gliethttp =14==

drwx------ 2 www-data www-data 4.0K 2010-03-28 14:35 luther.gliethttp =16==
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息