---
title: "Linux/FreeBSD下用C语言开发PHP的so扩展模块例解"
author: "yangzb"
date: "2009-06-06T13:26:22Z"
tags: ["C", "C#", "C++", "FreeBSD", "PHP"]
slug: "linux-freebsd-c-php-so"
draft: false
lastmod: "2009-06-06T13:29:36Z"
id: 403207
url: "https://yangzb.iteye.com/blog/403207"
---

我从 97 年接触互联网的 web 开发，至今已经过去 9 年了，从最初的 frontpage 做 html 页面到学会 ASP+access+IIS 开始，就跟 web 开发干上了，后来又依次使用了 ASP+SQLServer+IIS 、 JSP+Oracle+Jrun(Resin/Tomcat) 、 PHP+Syabse(MySQL)+Apache … 最后我定格到了 PHP+MySQL+Apache+Linux(BSD) 的架构上，也就是大家常说的 LAMP 架构，这说来有很多理由，网上也有很多人讨论各种架构和开发语言之间的优劣，我就不多说了，简单说一下我喜欢 LAMP 的几个主要原因：

1 、全开放的免费平台；  
2 、简单易上手、各种资源丰富；  
3 、 PHP 、 MySQL 、 Apache 与 Linux(BSD) 系统底层以及彼此间无缝结合，非常高效；  
4 、均使用最高效的语言 C/C++ 开发，性能可靠；  
5 、 PHP 语言和 C 的风格基本一致，还吸取了 Java 和 C++ 的诸多架构优点；  
6 、这是最关键的一点，那就是 PHP 可以非常方便的使用 C/C++ 开发扩展模块，给了 PHP 无限的扩张性！

基于以上原因，我非常喜欢基于 PHP 语言的架构，其中最关键的一点就是最后一点，以前在 Yahoo 和 mop 均推广使用这个平台，在 C 扩展 php 方面也有一些经验，在此和大家分享一下，希望可以抛砖引玉。

用 C 语言编写 PHP 的扩展模块的方法有几种，根据最后的表现形式有两种，一种是直接编译进 php ，一种是编译为 php 的 so 扩展模块来被 php 调用，另外根据编译的方式有两种，一种使用 phpize 工具（ php 编译后有的），一种使用 ext\_skel 工具（ php 自带的），我们使用最多，也是最方便的方式就是使用 ext\_skel 工具来编写 php 的 so 扩展模块，这里也主要介绍这种方式。

我们在 php 的源码目录里面可以看到有个 ext 目录（我这里说的 php 都是基于 Linux 平台的 php 来说的，不包括 windows 下的），在 ext 目录下有个工具 ext\_skel ，这个工具可以让我们简单的开发出 php 的扩展模块，它提供了一个通用的 php 扩展模块开发步骤和模板。下面我们以开发一个在 php 里面进行 utf8/gbk/gb2312 三种编码转换的扩展模块为例子进行说明。在这个模块中，我们要最终提供以下几个函数接口：

(1) string toplee\_big52gbk(string s)  
将输入字符串从 BIG5 码转换成 GBK  
(2) string toplee\_gbk2big5(string s)  
将输入字符串从 GBK 转换成 BIG5 码  
(3) string toplee\_normalize\_name(string s)  
将输入字符串作以下处理：全角转半角， strim ，大写转小写  
(4) string toplee\_fan2jian(int code, string s)  
将输入的 GBK 繁体字符串转换成简体  
(5) string toplee\_decode\_utf(string s)  
将 utf 编码的字符串转换成 UNICODE  
(6) string toplee\_decode\_utf\_gb(string s)  
将 utf 编码的字符串转换成 GB  
(7) string toplee\_decode\_utf\_big5(string s)  
将 utf 编码的字符串转换成 BIG5  
(8) string toplee\_encode\_utf\_gb(string s)  
将输入的 GBKf 编码的字符串转换成 utf 编码

首先，我们进入 ext 目录下，运行下面命令：  
#./ext\_skel –extname=toplee  
这时， php 会自动在 ext 目录下为我们生成一个目录 toplee ，里面包含下面几个文件  
.cvsignore  
CREDITS  
EXPERIMENTAL  
config.m4  
php\_toplee.h  
tests  
toplee.c  
toplee.php

 

其中最有用的就是 config.m4 和 toplee.c 文件  
接下来我们修改 config.m4 文件  
#vi ./config.m4  
找到里面有类似这样几行

1.       dnl PHP\_ARG\_WITH(toplee, for toplee support,

2.       dnl Make sure that the comment is aligned:

3.       dnl \[ --with-toplee Include toplee support])

4.        

5.       dnl Otherwise use enable:

6.        

7.       dnl PHP\_ARG\_ENABLE(toplee, whether to enable toplee support,

8.       dnl Make sure that the comment is aligned:

9.       dnl \[ --enable-toplee Enable toplee support])

上面的几行意思是说告诉 php 编译的使用使用那种方式加载我们的扩展模块 toplee ，我们使用 –with-toplee 的方式，于是我们修改为下面的样子

1.       PHP\_ARG\_WITH(toplee, for toplee support,

2.       Make sure that the comment is aligned:

3.       \[ --with-toplee Include toplee support])

4.        

5.       dnl Otherwise use enable:

6.        

7.       dnl PHP\_ARG\_ENABLE(toplee, whether to enable toplee support,

8.       dnl Make sure that the comment is aligned:

9.       dnl \[ --enable-toplee Enable toplee support])

然后我们要做的关键事情就是编写 toplee.c ，这个是我们编写模块的主要文件，如果您什么都不修改，其实也完成了一个 php 扩展模块的编写，里面有类似下面的几行代码

1.       PHP\_FUNCTION ( confirm\_toplee\_compiled )

2.       {

3.               char * arg = NULL ;

4.               int arg\_len , len ;

5.               char string \[ 256 ] ;

6.        

7.               if ( zend\_parse\_parameters ( ZEND\_NUM\_ARGS () TSRMLS\_CC , " s " , & arg , & arg\_len ) == FAILURE ) {

8.                       return ;

9.               }

10.    

11.           len = sprintf ( string , " Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP. " , " toplee " , arg ) ;

12.           RETURN\_STRINGL ( string , len , 1 ) ;

13.   }

如果我们在后面完成 php 的编译时把新的模块编译进去，那么我们就可以在 php 脚本中调用函数 toplee() ，它会输出一段字符串 “Congratulations! You have successfully modified ext/toplee/config.m4. Module toplee is now compiled into PHP.”

下面是我们对 toplee.c 的修改，让其支持我们预先规划的功能和接口，下面是 toplee.c 的源代码

1.       /*

2.         +----------------------------------------------------------------------+

3.         | PHP Version 4                                                        |

4.         +----------------------------------------------------------------------+

5.         | Copyright (c) 1997-2002 The PHP Group                                |

6.         +----------------------------------------------------------------------+

7.         | This source file is subject to version 2.02 of the PHP license,      |

8.         | that is bundled with this package in the file LICENSE, and is        |

9.         | available at through the world-wide-web at                           |

10.     | http://www.php.net/license/2\_02.txt.                                 |

11.     | If you did not receive a copy of the PHP license and are unable to   |

12.     | obtain it through the world-wide-web, please send a note to          |

13.     | license@php.net so we can mail you a copy immediately.               |

14.     +----------------------------------------------------------------------+

15.     | Author:                                                              |

16.     +----------------------------------------------------------------------+

17.    

18.    $Id: header,v 1.10 2002/02/28 08:25:27 sebastian Exp $  

19.   \*/

20.    

21.   #ifdef HAVE\_CONFIG\_H

22.   #include " config.h "

23.   #endif

24.    

25.   #include " php.h "

26.   #include " php\_ini.h "

27.   #include " ext/standard/info.h "

28.   #include " php\_gbk.h "

29.   #include " toplee\_util.h "

30.    

31.   /* If you declare any globals in php\_gbk.h uncomment this:

32.   ZEND\_DECLARE\_MODULE\_GLOBALS(gbk)

33.   \*/

34.    

35.   /* True global resources - no need for thread safety here \*/

36.   static int le\_gbk ;

37.    

38.   /* {{{ gbk\_functions\[]

39.   *

40.   * Every user visible function must have an entry in gbk\_functions\[].

41.   \*/

42.   function\_entry gbk\_functions \[] = {

43.       PHP\_FE ( toplee\_decode\_utf ,    NULL )

44.       PHP\_FE ( toplee\_decode\_utf\_gb ,    NULL )

45.       PHP\_FE ( toplee\_decode\_utf\_big5 ,    NULL )

46.       PHP\_FE ( toplee\_encode\_utf\_gb ,    NULL )

47.    

48.       PHP\_FE ( toplee\_big52gbk ,    NULL )

49.       PHP\_FE ( toplee\_gbk2big5 ,    NULL )

50.       PHP\_FE ( toplee\_fan2jian ,    NULL )

51.       PHP\_FE ( toplee\_normalize\_name ,    NULL )

52.       { NULL , NULL , NULL }     /* Must be the last line in gbk\_functions\[] \*/

53.   } ;

54.   /* }}} \*/

55.    

56.   /* {{{ gbk\_module\_entry

57.   \*/

58.   zend\_module\_entry gbk\_module\_entry = {

59.   #if ZEND\_MODULE\_API\_NO &gt;= 20010901

60.       STANDARD\_MODULE\_HEADER ,

61.   #endif

62.       " gbk " ,

63.       gbk\_functions ,

64.       PHP\_MINIT ( gbk ) ,

65.       PHP\_MSHUTDOWN ( gbk ) ,

66.       PHP\_RINIT ( gbk ) ,        /* Replace with NULL if there's nothing to do at request start \*/

67.       PHP\_RSHUTDOWN ( gbk ) ,    /* Replace with NULL if there's nothing to do at request end \*/

68.       PHP\_MINFO ( gbk ) ,

69.   #if ZEND\_MODULE\_API\_NO &gt;= 20010901

70.       " 0.1 " , /* Replace with version number for your extension \*/

71.   #endif

72.       STANDARD\_MODULE\_PROPERTIES

73.   } ;

74.   /* }}} \*/

75.    

76.   #ifdef COMPILE\_DL\_GBK

77.   ZEND\_GET\_MODULE ( gbk )

78.   #endif

79.    

80.   /* {{{ PHP\_INI

81.   \*/

82.   /* Remove comments and fill if you need to have entries in php.ini\*/

83.   PHP\_INI\_BEGIN ()

84.       PHP\_INI\_ENTRY ( " gbk2uni " ,            "" ,        PHP\_INI\_SYSTEM ,    NULL )

85.       PHP\_INI\_ENTRY ( " uni2gbk " ,            "" ,        PHP\_INI\_SYSTEM ,    NULL )

86.       PHP\_INI\_ENTRY ( " uni2big5 " ,            "" ,        PHP\_INI\_SYSTEM ,    NULL )

87.       PHP\_INI\_ENTRY ( " big52uni " ,            "" ,        PHP\_INI\_SYSTEM ,    NULL )

88.       PHP\_INI\_ENTRY ( " big52gbk " ,            "" ,        PHP\_INI\_SYSTEM ,    NULL )

89.       PHP\_INI\_ENTRY ( " gbk2big5 " ,            "" ,        PHP\_INI\_SYSTEM ,    NULL )

90.   //    STD\_PHP\_INI\_ENTRY("gbk.global\_value",      "42", PHP\_INI\_ALL, OnUpdateInt, global\_value, zend\_gbk\_globals, gbk\_globals)

91.   //    STD\_PHP\_INI\_ENTRY("gbk.global\_string", "foobar", PHP\_INI\_ALL, OnUpdateString, global\_string, zend\_gbk\_globals, gbk\_globals)

92.   PHP\_INI\_END ()

93.    

94.   /* }}} \*/

95.    

96.   /* {{{ php\_gbk\_init\_globals

97.   \*/

98.   /* Uncomment this function if you have INI entries

99.   static void php\_gbk\_init\_globals(zend\_gbk\_globals \*gbk\_globals)

100.    {

101.        gbk\_globals-&gt;global\_value = 0;

102.        gbk\_globals-&gt;global\_string = NULL;

103.    }

104.    \*/

105.    /* }}} \*/

106.     

107.    char gbk2uni\_file \[ 256 ] ;

108.    char uni2gbk\_file \[ 256 ] ;

109.    char big52uni\_file \[ 256 ] ;

110.    char uni2big5\_file \[ 256 ] ;

111.    char gbk2big5\_file \[ 256 ] ;

112.    char big52gbk\_file \[ 256 ] ;

113.     

114.    //utf file init flag

115.    static int initutf = 0 ;

116.     

117.    /* {{{ PHP\_MINIT\_FUNCTION

118.    \*/

119.    PHP\_MINIT\_FUNCTION ( gbk )

120.    {

121.        /* If you have INI entries, uncomment these lines

122.        ZEND\_INIT\_MODULE\_GLOBALS(gbk, php\_gbk\_init\_globals, NULL);\*/

123.        REGISTER\_INI\_ENTRIES () ;

124.        memset ( gbk2uni\_file , 0 , sizeof ( gbk2uni\_file )) ;

125.        memset ( uni2gbk\_file , 0 , sizeof ( uni2gbk\_file )) ;

126.        memset ( big52uni\_file , 0 , sizeof ( big52uni\_file )) ;

127.        memset ( uni2big5\_file , 0 , sizeof ( uni2big5\_file )) ;

128.        memset ( gbk2big5\_file , 0 , sizeof ( gbk2big5\_file )) ;

129.        memset ( big52gbk\_file , 0 , sizeof ( big52gbk\_file )) ;

130.       

131.        strncpy ( gbk2uni\_file , INI\_STR ( " gbk2uni " ) , sizeof ( gbk2uni\_file ) - 1 ) ;

132.        strncpy ( uni2gbk\_file , INI\_STR ( " uni2gbk " ) , sizeof ( uni2gbk\_file ) - 1 ) ;

133.        strncpy ( big52uni\_file , INI\_STR ( " big52uni " ) , sizeof ( big52uni\_file ) - 1 ) ;

134.        strncpy ( uni2big5\_file , INI\_STR ( " uni2big5 " ) , sizeof ( uni2big5\_file ) - 1 ) ;

135.        strncpy ( gbk2big5\_file , INI\_STR ( " gbk2big5 " ) , sizeof ( uni2big5\_file ) - 1 ) ;

136.        strncpy ( big52gbk\_file , INI\_STR ( " big52gbk " ) , sizeof ( uni2big5\_file ) - 1 ) ;

137.     

138.        //InitMMResource();

139.        InitResource () ;

140.        if (( uni2gbk\_file \[ 0 ] == '\\ 0 ' ) || ( uni2big5\_file \[ 0 ] == '\\ 0 ' )

141.          || ( gbk2big5\_file \[ 0 ] == '\\ 0 ' ) || ( big52gbk\_file \[ 0 ] == '\\ 0 ' )

142.          || ( gbk2uni\_file \[ 0 ] == '\\ 0 ' ) || ( big52uni\_file \[ 0 ] == '\\ 0 ' ))

143.        {  

144.            return FAILURE ;

145.        }

146.     

147.        if ( gbk2uni\_file \[ 0 ] != '\\ 0 ' )

148.        {

149.            if ( LoadOneCodeTable ( CODE\_GBK2UNI , gbk2uni\_file ) != NULL )

150.            {

151.                toplee\_cleanup\_mmap ( NULL ) ;

152.                return FAILURE ;

153.            }

154.        }

155.     

156.        if ( uni2gbk\_file \[ 0 ] != '\\ 0 ' )

157.        {

158.            if ( LoadOneCodeTable ( CODE\_UNI2GBK , uni2gbk\_file ) != NULL )

159.            {

160.                toplee\_cleanup\_mmap ( NULL ) ;

161.                return FAILURE ;

162.            }

163.        }

164.     

165.        if ( big52uni\_file \[ 0 ] != '\\ 0 ' )

166.        {

167.            if ( LoadOneCodeTable ( CODE\_BIG52UNI , big52uni\_file ) != NULL )

168.            {

169.                toplee\_cleanup\_mmap ( NULL ) ;

170.                return FAILURE ;

171.            }

172.        }

173.     

174.        if ( uni2big5\_file \[ 0 ] != '\\ 0 ' )

175.        {

176.            if ( LoadOneCodeTable ( CODE\_UNI2BIG5 , uni2big5\_file ) != NULL )

177.            {

178.                toplee\_cleanup\_mmap ( NULL ) ;

179.                return FAILURE ;

180.            }

181.        }

182.       

183.        if ( gbk2big5\_file \[ 0 ] != '\\ 0 ' )

184.        {

185.            if ( LoadOneCodeTable ( CODE\_GBK2BIG5 , gbk2big5\_file ) != NULL )

186.            {

187.                toplee\_cleanup\_mmap ( NULL ) ;

188.                return FAILURE ;

189.            }

190.        }

191.     

192.        if ( big52gbk\_file \[ 0 ] != '\\ 0 ' )

193.        {

194.            if ( LoadOneCodeTable ( CODE\_BIG52GBK , big52gbk\_file ) != NULL )

195.            {

196.                toplee\_cleanup\_mmap ( NULL ) ;

197.                return FAILURE ;

198.            }

199.        }

200.       

201.        initutf = 1 ;

202.        return SUCCESS ;

203.    }

204.    /* }}} \*/

205.     

206.    /* {{{ PHP\_MSHUTDOWN\_FUNCTION

207.    \*/

208.    PHP\_MSHUTDOWN\_FUNCTION ( gbk )

209.    {

210.        /* uncomment this line if you have INI entries\*/

211.        UNREGISTER\_INI\_ENTRIES () ;

212.       

213.        toplee\_cleanup\_mmap ( NULL ) ;

214.        return SUCCESS ;

215.    }

216.    /* }}} \*/

217.     

218.    /* Remove if there's nothing to do at request start \*/

219.    /* {{{ PHP\_RINIT\_FUNCTION

220.    \*/

221.    PHP\_RINIT\_FUNCTION ( gbk )

222.    {

223.        return SUCCESS ;

224.    }

225.    /* }}} \*/

226.     

227.    /* Remove if there's nothing to do at request end \*/

228.    /* {{{ PHP\_RSHUTDOWN\_FUNCTION

229.    \*/

230.    PHP\_RSHUTDOWN\_FUNCTION ( gbk )

231.    {

232.        return SUCCESS ;

233.    }

234.    /* }}} \*/

235.     

236.    /* {{{ PHP\_MINFO\_FUNCTION

237.    \*/

238.    PHP\_MINFO\_FUNCTION ( gbk )

239.    {

240.        php\_info\_print\_table\_start () ;

241.        php\_info\_print\_table\_header ( 2 , " gbk support " , " enabled " ) ;

242.        php\_info\_print\_table\_end () ;

243.     

244.        /* Remove comments if you have entries in php.ini\*/

245.        DISPLAY\_INI\_ENTRIES () ;

246.       

247.    }

248.    /* }}} \*/

249.     

250.     

251.    /* Remove the following function when you have succesfully modified config.m4

252.       so that your module can be compiled into PHP, it exists only for testing

253.       purposes. \*/

254.     

255.    /* {{{ proto  toplee\_decode\_utf(string s)

256.        \*/

257.    PHP\_FUNCTION ( toplee\_decode\_utf )

258.    {

259.        char * s = NULL , * t = NULL ;

260.        int argc = ZEND\_NUM\_ARGS () ;

261.        int s\_len ;

262.     

263.        if ( zend\_parse\_parameters ( argc TSRMLS\_CC , " s " , & s , & s\_len ) == FAILURE )  

264.            return ;

265.     

266.        if ( ! initutf )

267.            RETURN\_FALSE

268.        t = strdup ( s ) ;

269.        if ( t == NULL )

270.            RETURN\_FALSE

271.     

272.     

273.        DecodePureUTF ( t , KEEP\_UNICODE ) ;

274.        RETVAL\_STRING ( t , 1 ) ;

275.        free ( t ) ;

276.        return ;

277.    }

278.    /* }}} \*/

279.     

280.    /* {{{ proto  toplee\_decode\_utf\_gb(string s)

281.        \*/

282.    PHP\_FUNCTION ( toplee\_decode\_utf\_gb )

283.    {

284.        char * s = NULL , * t = NULL ;

285.        int argc = ZEND\_NUM\_ARGS () ;

286.        int s\_len ;

287.     

288.        if ( zend\_parse\_parameters ( argc TSRMLS\_CC , " s " , & s , & s\_len ) == FAILURE )  

289.            return ;

290.     

291.        if ( ! initutf )

292.            RETURN\_FALSE

293.        t = strdup ( s ) ;

294.        if ( t == NULL )

295.            RETURN\_FALSE

296.     

297.        DecodePureUTF ( t , DECODE\_UNICODE ) ;

298.        RETVAL\_STRING ( t , 1 ) ;

299.        free ( t ) ;

300.        return ;

301.     

302.    }

303.    /* }}} \*/

304.     

305.    /* {{{ proto  toplee\_decode\_utf\_big5(string s)

306.        \*/

307.    PHP\_FUNCTION ( toplee\_decode\_utf\_big5 )

308.    {

309.        char * s = NULL , * t = NULL ;

310.        int argc = ZEND\_NUM\_ARGS () ;

311.        int s\_len ;

312.     

313.        if ( zend\_parse\_parameters ( argc TSRMLS\_CC , " s " , & s , & s\_len ) == FAILURE )  

314.            return ;

315.     

316.        if ( ! initutf )

317.            RETURN\_FALSE

318.        t = strdup ( s ) ;

319.        if ( t == NULL )

320.            RETURN\_FALSE

321.     

322.     

323.        DecodePureUTF ( t , DECODE\_UNICODE | DECODE\_BIG5 ) ;

324.        RETVAL\_STRING ( t , 1 ) ;

325.        free ( t ) ;

326.        return ;