用c为php写扩展函数

4.新建扩展文件

cd ~/php.5.4.9/ext/

./ext_skel --extname=firstapp    //firstapp就是我需要创建的一个模块名

这时在ext文件下生成一个firstapp文件夹

cd firstapp

(1)修改config.m4文件

vi config.m4

查找一下字符串,并去掉行前面的 dnl ,如下这样,保存退出

PHP_ARG_ENABLE(firstapp, whether to enable firstapp support,

Make sure that the comment is aligned:

[  --enable-firstapp           Enable firstapp support])

 

(2)修改php-firstapp.h

vi php-firstapp.h

查找如下字符串,并在其下添加如下方法(蓝色为添加项)

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

PHP_FUNCTION(firstapp);

 

(3)修改firstapp.c文件

vi firstapp.c

查找如下字符串,并添加如下方法:(蓝色为添加项)

zend_function_entry firstapp_functions[] = {

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

        PHP_FE(firstapp,NULL)

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

};

在firstapp.c文件的最后添加如下方法:PHP_FUNCTION(firstapp){

        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, "Congratulations! You have successfull", "firstapp", arg);

     RETURN_STRINGL(strg, len, 0);

}

以上内容可以复制firstapp.c中PHP_FUNCTION(confirm_firstapp_compiled)方法的内容

5.此时扩展文件新建完成,下面就对其进行编译

 

[root@localhost firstapp]# /usr/local/php/bin/phpize

[root@localhost firstapp]# ./configure --with-php-config=/usr/bin/php-config 

[root@localhost firstapp]# make

[root@localhost firstapp]# make install

此时firstapp.so文件已经在/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/

[root@localhost firstapp]# cp /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/firstapp.so /usr/lib/php/modules/

 

6.此时动态库已经生成了,最后进行php.ini 文件的配置

vi /usr/local/php/lib/php.ini

添加 动态库路径和模块名

extension_dir="/usr/lib/php/modules"

 

extension=firstapp.so

 

7.测试firstapp.so模块

[root@localhost firstapp]# php -m

[PHP Modules]

ctype

date

dom

filter

firstapp

hash

iconv

.........

新建firstapp.php文件

<?

echo firstapp("Falcon.C");

?>

 

[root@localhost firstapp]# php firstapp.php

Congratulations! You have successfull   

[root@localhost firstapp]# 

 

在此表示firstapp.so模块添加成功并正常运行

原文地址:https://www.cnblogs.com/lsl8966/p/2811370.html