如何用C语言编写PHP扩展的详解

Python08

如何用C语言编写PHP扩展的详解,第1张

1:预定义

在home目录,也可以其他任意目录,写一个文件,例如caleng_module.def

内容是你希望定义的函数名以及参数:

int a(int x,int y)

string b(string str,int n)

2:到php源码目录的ext目录

#cd /usr/local/php-5.4.0/ext/

执行命令,生成对应扩展目录

#./ext_skel --extname=caleng_module --proto=/home/hm/caleng_module.def

3:修改config.m4

去掉dnl的注释

PHP_ARG_ENABLE(caleng_module, whether to enable caleng_module support,

Make sure that the comment is aligned:

[  --enable-caleng_module           Enable caleng_module support])

4:修改caleng_module.c

代码如下:

/* {{{ proto int a(int x, int y)

   */

PHP_FUNCTION(a)

{

int argc = ZEND_NUM_ARGS()

int x

int y

   int z

if (zend_parse_parameters(argc TSRMLS_CC, "ll", &x, &y) == FAILURE)

 return

z=x+y

 RETURN_LONG(z)

}

/* }}} */

/* {{{ proto string b(string str, int n)

   */

PHP_FUNCTION(b)

{

char *str = NULL

   int argc = ZEND_NUM_ARGS()

   int str_len

   long n

   char *result

   char *ptr

   int result_length

   if (zend_parse_parameters(argc TSRMLS_CC, "sl", &str, &str_len, &n) == FAILURE)

       return

   result_length = str_len * n

   result = (char *) emalloc(result_length + 1)

   ptr = result

   while (n--) {

       memcpy(ptr, str, str_len)

       ptr += str_len

   }

   *ptr = '\0'

   RETURN_STRINGL(result, result_length, 0)

}

/* }}} */

5:生成扩展库

#cd ./caleng_module

#/usr/local/php/bin/phpize

#./configure --with-php-config=/usr/local/php/bin/php-config

#make

#make install

6:到php的对应extensions目录

如上图所示

#cd /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/

改目录下有生成的caleng_module.so文件

7:修改php.ini

php.ini如果找不到可以从phpinfo()打出的信息看到

#cd /usr/local/php/lib/

php.ini增加扩展信息

extension=caleng_module.so

8:重启Apache

# /usr/local/apache2/bin/apachectl restart

9:检查加载

/usr/local/php/bin/php -m

10:PHP调用

代码如下:

echo a(1,2)

输出 3  就说明成功了!

下面是原文

Linux下用C开发PHP扩展

一、首先下载PHP源码包,假设源码包目录为:/software/php-5.2.13

一、首先下载PHP源码包,假设源码包目录为:/software/php-5.2.13

#>cd /software/php-5.2.13/ext

二、假设我们要开发一个名为caleng_module的扩展,该扩展包含两个函数:a--处理两个整型相加和b-处理字符串重复输出;

1、首先编写一个函数定义文件,该文件编写函数原型后缀为def,假设为:caleng_module.def

int a(int x, int y)

string b(string str, int n)

2、通过扩展骨架生成器,将在ext目录下自动建立扩展目录caleng_module

#>./ext_skel --extname=caleng_module --proto=caleng_module.def

3、修改配置文件: #>vim /software/php-5.2.13/ext/caleng_module/config.m4,将如下行的注释标签"dnl"去掉,修改后如下所示:

PHP_ARG_ENABLE(myfunctions, whether to enable myfunctions support,

Make sure that the comment is aligned:

[  --enable-myfunctions           Enable myfunctions support])

4、完善函数a和b的功能: #>vim /software/php-5.2.13/ext/caleng_module/caleng_module.c

PHP_FUNCTION(a)

{

   int x, y, z

   int argc = ZEND_NUM_ARGS()

   if (zend_parse_parameters(argc TSRMLS_CC, "ll", &x, &y) == FAILURE)

       return

   z = x + y

   RETURN_LONG(z)

}

PHP_FUNCTION(b)

{

   char *str = NULL

   int argc = ZEND_NUM_ARGS()

   int str_len

   long n

   char *result

   char *ptr

   int result_length

   if (zend_parse_parameters(argc TSRMLS_CC, "sl", &str, &str_len, &n) == FAILURE)

       return

   result_length = str_len * n

   result = (char *) emalloc(result_length + 1)

   ptr = result

   while (n--) {

       memcpy(ptr, str, str_len)

       ptr += str_len

   }

   *ptr = '\0'

   RETURN_STRINGL(result, result_length, 0)

}

三、编译安装,假设php的安装目录为:/usr/localhost/webserver/php

#>cd /software/php-5.2.13/ext/caleng_module

#>/usr/localhost/webserver/php/bin/phpize

#>./configure --with-php-config=/usr/localhost/webserver/php/bin/php-config

#>make

#>make install

现在将在/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613目录下生成caleng_module.so文件

在php.ini配置文件中加入: extension=caleng_module.so.

如何使用C语言开发PHP扩展。

函数功能:php里面的整数是有符号数,其内部实现其实就是long,不是unsigned long。对于32位机器来说,php最大能表示的整数就是2^31-1了,一般在应用中碰到大于2^31-1而小于2^32的数就只能用字符串来表示了。对于mixed int_ext(string in)来说,如果字符串in表示的整数小于2^31-1,那么就返回整数,如果大于就返回字符串。

开发扩展步骤如下:(首先需要下载php的源码,这里下载的是php-5.3.14)

1,建立扩展骨架

[plain] view plaincopyprint?

01.cd php-5.3.14/ext

02../ext_skel --extname=int_ext

cd php-5.3.14/ext

./ext_skel --extname=int_ext

2,修改编译参数

[plain] view plaincopyprint?

01.cd php-5.3.14/ext/int_ext

02.vi config.m4

cd php-5.3.14/ext/int_ext

vi config.m4去掉 PHP_ARG_ENABLE(int_ext, whether to enable int_ext support 和

[ --enable-int_ext Enable int_ext support]) 两行前面的dnl 修改后为:

[plain] view plaincopyprint?

01.1. dnl Otherwise use enable:

02.2. PHP_ARG_ENABLE(int_ext, whether to enable int_ext support,

03.3. dnl Make sure that the comment is aligned:

04.4. [ --enable-int_ext Enable int_ext support])

1. dnl Otherwise use enable:

2. PHP_ARG_ENABLE(int_ext, whether to enable int_ext support,

3. dnl Make sure that the comment is aligned:

4. [ --enable-int_ext Enable int_ext support])

3,编写C代码

[plain] view plaincopyprint?

01.cd php-5.3.14/ext/int_ext

02.vi php_int_ext.h

03.#在 PHP_FUNCTION(confirm_int_ext_compiled)后面新增一行 PHP_FUNCTION(int_ext)

cd php-5.3.14/ext/int_ext

vi php_int_ext.h

#在 PHP_FUNCTION(confirm_int_ext_compiled)后面新增一行 PHP_FUNCTION(int_ext)[plain] view plaincopyprint?

01.cd php-5.3.14/ext/int_ext

02.vi int_ext.c

03.#在PHP_FE(confirm_int_ext_compiled, NULL) 后面添加 PHP_FE(int_ext, NULL)添加后为:

04.1. zend_function_entry int_ext_functions[] = {

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

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

07.4. {NULL, NULL, NULL} /* Must be the last line in int_ext_functions[] */

08.5. }

cd php-5.3.14/ext/int_ext

vi int_ext.c

#在PHP_FE(confirm_int_ext_compiled, NULL) 后面添加 PHP_FE(int_ext, NULL)添加后为:

1. zend_function_entry int_ext_functions[] = {

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

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

4. {NULL, NULL, NULL} /* Must be the last line in int_ext_functions[] */

5. }

核心代码:

[plain] view plaincopyprint?

01.PHP_FUNCTION(int_ext)

02.{

03.char * str = NULL

04.int str_len

05.int argc = ZEND_NUM_ARGS()

06.if(zend_parse_parameters(argc TSRMLS_CC,"s",&str,&str_len) == FAILURE)

07.return

08.char * result

09.int result_length = str_len

10.result = (char *) emalloc(result_length + 1)

11.memcpy(result,str,result_length)

12.unsigned long result_num = strtoul(result, NULL, 10)

13.int sizeoflong sizeof(long)

14.unsigned long max_long = 1 <<(sizeoflong * 8 -1)

15.if(result_num <max_long)

16.{

17.RETURN_LONG(result_num)

18.}

19.else

20.{

21. RESULT_STRINGL(result, result_length, 0)

22. }

23.}

PHP_FUNCTION(int_ext)

{

char * str = NULL

int str_len

int argc = ZEND_NUM_ARGS()

if(zend_parse_parameters(argc TSRMLS_CC,"s",&str,&str_len) == FAILURE)

return

char * result

int result_length = str_len

result = (char *) emalloc(result_length + 1)

memcpy(result,str,result_length)

unsigned long result_num = strtoul(result, NULL, 10)

int sizeoflong sizeof(long)

unsigned long max_long = 1 <<(sizeoflong * 8 -1)

if(result_num <max_long)

{

RETURN_LONG(result_num)

}

else

{

RESULT_STRINGL(result, result_length, 0)

}

}

4,编译

[plain] view plaincopyprint?

01.cd php-5.3.14/ext/int_ext

02./usr/local/php/bin/pphpize

03../configure --with-php-config=/usr/local/php/bin/php-config

04.make

05.make install

cd php-5.3.14/ext/int_ext

/usr/local/php/bin/pphpize

./configure --with-php-config=/usr/local/php/bin/php-config

make

make install

此时会产生一个so文件: /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/int_ext.so

修改php.ini 添加扩展extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"

[int_ext]

extension = int_ext.so

5,测试

[plain] view plaincopyprint?

01.$a = int_ext("12345678900")

02.var_dump($a)

03.$a = int_ext("123456789")

04.var_dump($a)

$a = int_ext("12345678900")

var_dump($a)

$a = int_ext("123456789")

var_dump($a)

结果输出:

[plain] view plaincopyprint?

01.string(11) "12345678900"

02.int(123456789)

C语言是一门通用性的语言,并没有针对某个领域进行优化,在实际项目中,C语言主要用于较底层的开发,例如:

Windows、Linux、Unix 等操作系统的内核90%以上都使用C语言开发;

开发硬件驱动,让硬件和操作系统连接起来,这样用户才能使用硬件、程序员才能控制硬件;

单片机和嵌入式属于软硬件的结合,有很多使用C语言的地方;

开发系统组件或服务,用于支撑上层应用;

编写PHP扩展,增强PHP的功能;

如果对软件某个模块(例如算法和搜索部分)的效率要求较高,也可以使用C语言来开发。