您的位置:首页 > 编程语言 > PHP开发

Windows 下开发PHP扩展DLL

2016-08-28 17:39 393 查看
一、开始之前

1. 系统环境:Windows 7

2. 开发工具:Microsoft Visual Studio 2010

3. AP环境:即已经存在的Apache2.2x(VC9)、PHP5.3x(VC9)环境。

4. PHP源码:下载稳定版本源码,解压到如:C:Program Filesphpsrc 下。本示例用的是PHP5.3.8。

5. 配置源码:将源码中src/win32/build/config.w32.h.in文件拷贝一份到src/main/下,并重命名为:config.w32.h。

二、创建项目

启动Microsoft Visual Studio 2010(我用的是Professional版),新建一个项目,如:php_screw,类型选择如下图:

新建项目第一步



新建项目第二步



点击“finish”,即可看到在IDE右边多出了Solution Explorer和包含的默认项目文件结构。如下图:

默认项目文件结构



三、新建程序文件并设置项目属性

1. 在Source Files下创建一个cpp文件。



2. 选中项目名称,点右键,选择“Properties”打开属性设置对话框,进行属性设计,如下图:

打开属性对话框



设置Additional Include Directories

 


设置 Preprocessor Definitions

 


设置Additional Library Directories



设置 Additional Dependencies



四、编写示例代码并Build项目

1. 编写如下示例代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50
/**

 * 示例代码

 **/

#define PHP_COMPILER_ID  "VC9"

#include "php.h"

#include "ext/standard/info.h"

 

ZEND_FUNCTION(example);

PHP_MINFO_FUNCTION(example);

 

zend_function_entry use_functions[] = {

    ZEND_FE(example, NULL)

    {NULL, NULL, NULL}

};

 

zend_module_entry example_module_entry = {

    STANDARD_MODULE_HEADER,

    "PHP Extension Example",

    use_functions,

    NULL, NULL, NULL, NULL,

    PHP_MINFO(example),

    "1.0 beta",

    STANDARD_MODULE_PROPERTIES

};

 

ZEND_GET_MODULE(example);

 

ZEND_FUNCTION(example) {

     bool param;

 

     if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", ¶m) == FAILURE) {

         E_ERROR;

         return;

     }

 

     if(param) {

         php_printf("Example parm is true");

     } else {

         php_printf("Example parm is false");

     }

 

     return;

}

 

PHP_MINFO_FUNCTION(example) {

    php_info_print_table_start();

    php_info_print_table_header(2, "PHP Extension Example support", "enabled");

    php_info_print_table_row(2, "Version", "1.0 beta");

    php_info_print_table_end();

}

2. Build项目

保存好代码后,选择Build->Build Solution,一切正常的话,在下方图示的Output中可看到输出的信息,如图:



五、应用扩展

在项目的Debug文件夹下将生成的dll文件,拷贝到可正常工作的PHP ext文件夹下,并在php.ini上配置支持,重启Apache,在phpinfo中便可看到类似下图的信息:



编写PHP代码调用:

1

2

3

4

5

6
 

echo example(true);

//输出: Example parm is true

 

?>

完。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: