PHP 尽管提供了大量有用的函数,但是在特殊情况下还可能需要进行扩展编程,比如大量的 PECL(PHP Extension Community Library)就是以扩展的形式提供的(动态链接库dll文件),它们比 PEAR 的运行效率要高很多。
    PHP 扩展是用 C 或 C++ 编写的,需要编译成动态连接库 dll 文件后在 PHP 环境下注册后才能使用。
    编写 PHP 扩展的软件要求:
      VC++6.0 或 VC++.NET 环境。
      PHP 的源代码,需要编译。
    如果不愿意编译 PHP 的源代码,可以再下载 PHP 的已经编译成功的二进制代码(就是我们部署 PHP 运行环境的那些文件包)。注意分别下载的源文件包和已编译包,它们的版本必须一致。

过程:

1,安装 VC++6.0,并选择把其可执行文件路径加入环境变量中,使在命令行环境任意路径下可以运行编译器。
    2,安装 PHP 运行环境,并与 IIS 正确集成在一起。假设使用的 PHP 版本为 5.2.5,下载 php-5.2.5-Win32.zip 二进制包和 php-5.2.5.tar.gz 源代码包。安装环境为 C:\php-5.2.5-Win32。分别把源代码包和二进制包解压到该文件夹下。从 php.ini-recommended 拷贝生成一个 php.ini 文件。
    3,建立 C:\php-5.2.5-Win32\Release_TS 文件夹,拷贝 C:\php-5.2.5-Win32\dev\php5ts.lib 文件到这里。
    4,进入 C:\php-5.2.5-Win32\ext 文件夹,运行命令:
      C:\php-5.2.5-Win32\ext>..\php.exe ext_skel_win32.php --extname=myphpext
      Creating directory myphpext
      Creating basic files: config.m4 config.w32 .cvsignore myphpext.c php_myphpext.h
      CREDITS EXPERIMENTAL tests/001.phpt myphpext.php [done].

To use your new extension, you will have to execute the following steps:

1.  $ cd ..
      2.  $ vi ext/myphpext/config.m4
      3.  $ ./buildconf
      4.  $ ./configure --[with|enable]-myphpext
      5.  $ make
      6.  $ ./php -f ext/myphpext/myphpext.php
      7.  $ vi ext/myphpext/myphpext.c
      8.  $ make

Repeat steps 3-6 until you are satisfied with ext/myphpext/config.m4 and
      step 6 confirms that your module is compiled into PHP. Then, start writing
      code and repeat the last two steps as often as necessary.

结果在 ext 下生成一个文件夹 myphpext,包含一个 PHP 扩展应用编程框架。myphpext 可以任意取名,将来生成的 dll 文件格式为 php_[extname].dll,我们生成的就是 php_myphpext.dll。运行结果的提示信息 1.2...8 主要是对 Linux/Unix 环境而言的,我们不必理会。其实 config.m4 文件在 Windows 下也可能需要修改,但是对于我们简单的框架暂时还用不着。

文件夹 myphpext 包含若干个文件,其中:

myphpext.dsp 是工程文件,后边还要用;
    myphpext.php 扩展测试文件;
    php_myphpext.h 扩展函数定义头文件
    myphpext.c 扩展函数具体实现

以上 2 个重要的文件内容:

php_myphpext.h 文件:


/*
      +----------------------------------------------------------------------+
      | PHP Version 5                                                        |
      +----------------------------------------------------------------------+
      | Copyright (c) 1997-2007 The PHP Group                                |
      +----------------------------------------------------------------------+
      | This source file is subject to version 3.01 of the PHP license,      |
      | that is bundled with this package in the file LICENSE, and is        |
      | available through the world-wide-web at the following url:           |
      | http://www.php.net/license/3_01.txt                                  |
      | If you did not receive a copy of the PHP license and are unable to   |
      | obtain it through the world-wide-web, please send a note to          |
      | license@php.net so we can mail you a copy immediately.               |
      +----------------------------------------------------------------------+
      | Author:                                                              |
      +----------------------------------------------------------------------+
    */     /* $Id: header,v 1.16.2.1.2.1 2007/01/01 19:32:09 iliaa Exp $ */     #ifndef PHP_MYPHPEXT_H
    #define PHP_MYPHPEXT_H     extern zend_module_entry myphpext_module_entry;
    #define phpext_myphpext_ptr &myphpext_module_entry     #ifdef PHP_WIN32
    #define PHP_MYPHPEXT_API __declspec(dllexport)
    #else
    #define PHP_MYPHPEXT_API
    #endif     #ifdef ZTS
    #include "TSRM.h"
    #endif     PHP_MINIT_FUNCTION(myphpext);
    PHP_MSHUTDOWN_FUNCTION(myphpext);
    PHP_RINIT_FUNCTION(myphpext);
    PHP_RSHUTDOWN_FUNCTION(myphpext);
    PHP_MINFO_FUNCTION(myphpext);     PHP_FUNCTION(confirm_myphpext_compiled); /* For testing, remove later. */
    PHP_FUNCTION(HelloPHP);     /*
       Declare any global variables you may need between the BEGIN
     and END macros here:     ZEND_BEGIN_MODULE_GLOBALS(myphpext)
     long  global_value;
     char *global_string;
    ZEND_END_MODULE_GLOBALS(myphpext)
    */     /* In every utility function you add that needs to use variables
       in php_myphpext_globals, call TSRMLS_FETCH(); after declaring other
       variables used by that function, or better yet, pass in TSRMLS_CC
       after the last function argument and declare your utility function
       with TSRMLS_DC after the last declared argument.  Always refer to
       the globals in your function as MYPHPEXT_G(variable).  You are
       encouraged to rename these macros something shorter, see
       examples in any other php module directory.
    */     #ifdef ZTS
    #define MYPHPEXT_G(v) TSRMG(myphpext_globals_id, zend_myphpext_globals *, v)
    #else
    #define MYPHPEXT_G(v) (myphpext_globals.v)
    #endif     #endif /* PHP_MYPHPEXT_H */     /*
     * Local variables:
     * tab-width: 4
     * c-basic-offset: 4
     * End:
     * vim600: noet sw=4 ts=4 fdm=marker
     * vim<600: noet sw=4 ts=4
     */

myphpext.c 文件:


/*
      +----------------------------------------------------------------------+
      | PHP Version 5                                                        |
      +----------------------------------------------------------------------+
      | Copyright (c) 1997-2007 The PHP Group                                |
      +----------------------------------------------------------------------+
      | This source file is subject to version 3.01 of the PHP license,      |
      | that is bundled with this package in the file LICENSE, and is        |
      | available through the world-wide-web at the following url:           |
      | http://www.php.net/license/3_01.txt                                  |
      | If you did not receive a copy of the PHP license and are unable to   |
      | obtain it through the world-wide-web, please send a note to          |
      | license@php.net so we can mail you a copy immediately.               |
      +----------------------------------------------------------------------+
      | Author:                                                              |
      +----------------------------------------------------------------------+
    */     /* $Id: header,v 1.16.2.1.2.1 2007/01/01 19:32:09 iliaa Exp $ */     #ifdef HAVE_CONFIG_H
    #include "config.h"
    #endif     #include "php.h"
    #include "php_ini.h"
    #include "ext/standard/info.h"
    #include "php_myphpext.h"     /* If you declare any globals in php_myphpext.h uncomment this:
    ZEND_DECLARE_MODULE_GLOBALS(myphpext)
    */     /* True global resources - no need for thread safety here */
    static int le_myphpext;     /* {{{ myphpext_functions[]
     *
     * Every user visible function must have an entry in myphpext_functions[].
     */
    zend_function_entry myphpext_functions[] = {
     PHP_FE(confirm_myphpext_compiled, NULL)  /* For testing, remove later. */
     PHP_FE(HelloPHP, NULL)
     {NULL, NULL, NULL} /* Must be the last line in myphpext_functions[] */
    };
    /* }}} */     /* {{{ myphpext_module_entry
     */
    zend_module_entry myphpext_module_entry = {
    #if ZEND_MODULE_API_NO >= 20010901
     STANDARD_MODULE_HEADER,
    #endif
     "myphpext",
     myphpext_functions,
     PHP_MINIT(myphpext),
     PHP_MSHUTDOWN(myphpext),
     PHP_RINIT(myphpext),  /* Replace with NULL if there's nothing to do at request start */
     PHP_RSHUTDOWN(myphpext), /* Replace with NULL if there's nothing to do at request end */
     PHP_MINFO(myphpext),
    #if ZEND_MODULE_API_NO >= 20010901
     "0.1", /* Replace with version number for your extension */
    #endif
     STANDARD_MODULE_PROPERTIES
    };
    /* }}} */     #ifdef COMPILE_DL_MYPHPEXT
    ZEND_GET_MODULE(myphpext)
    #endif     /* {{{ PHP_INI
     */
    /* Remove comments and fill if you need to have entries in php.ini
    PHP_INI_BEGIN()
        STD_PHP_INI_ENTRY("myphpext.global_value",      "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_myphpext_globals, myphpext_globals)
        STD_PHP_INI_ENTRY("myphpext.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_myphpext_globals, myphpext_globals)
    PHP_INI_END()
    */
    /* }}} */     /* {{{ php_myphpext_init_globals
     */
    /* Uncomment this function if you have INI entries
    static void php_myphpext_init_globals(zend_myphpext_globals *myphpext_globals)
    {
     myphpext_globals->global_value = 0;
     myphpext_globals->global_string = NULL;
    }
    */
    /* }}} */     /* {{{ PHP_MINIT_FUNCTION
     */
    PHP_MINIT_FUNCTION(myphpext)
    {
     /* If you have INI entries, uncomment these lines
     REGISTER_INI_ENTRIES();
     */
     return SUCCESS;
    }
    /* }}} */     /* {{{ PHP_MSHUTDOWN_FUNCTION
     */
    PHP_MSHUTDOWN_FUNCTION(myphpext)
    {
     /* uncomment this line if you have INI entries
     UNREGISTER_INI_ENTRIES();
     */
     return SUCCESS;
    }
    /* }}} */     /* Remove if there's nothing to do at request start */
    /* {{{ PHP_RINIT_FUNCTION
     */
    PHP_RINIT_FUNCTION(myphpext)
    {
     return SUCCESS;
    }
    /* }}} */     /* Remove if there's nothing to do at request end */
    /* {{{ PHP_RSHUTDOWN_FUNCTION
     */
    PHP_RSHUTDOWN_FUNCTION(myphpext)
    {
     return SUCCESS;
    }
    /* }}} */     /* {{{ PHP_MINFO_FUNCTION
     */
    PHP_MINFO_FUNCTION(myphpext)
    {
     php_info_print_table_start();
     php_info_print_table_header(2, "myphpext support", "enabled");
     php_info_print_table_end();      /* Remove comments if you have entries in php.ini
     DISPLAY_INI_ENTRIES();
     */
    }
    /* }}} */     /* Remove the following function when you have succesfully modified config.m4
       so that your module can be compiled into PHP, it exists only for testing
       purposes. */     /* Every user-visible function in PHP should document itself in the source */
    /* {{{ proto string confirm_myphpext_compiled(string arg)
       Return a string to confirm that the module is compiled in */
    PHP_FUNCTION(confirm_myphpext_compiled)
    {
     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 successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "myphpext", arg);
     RETURN_STRINGL(strg, len, 0);
    }     PHP_FUNCTION(HelloPHP)
    {
     php_printf("Hello, PHP v5.2.5 - 2008-3-28");
    }     /* }}} */
    /* The previous line is meant for vim and emacs, so it can correctly fold and
       unfold functions in source code. See the corresponding marks just before
       function definition, where the functions purpose is also documented. Please
       follow this convention for the convenience of others editing your code.
    */     /*
     * Local variables:
     * tab-width: 4
     * c-basic-offset: 4
     * End:
     * vim600: noet sw=4 ts=4 fdm=marker
     * vim<600: noet sw=4 ts=4
     */

注意本例定义了一个函数 HelloPHP。在 php_myphpext.h 文件中定义:
    PHP_FUNCTION(HelloPHP);

在 myphpext.c 中有 2 处地方:

PHP_FE(HelloPHP, NULL) 语句把我们自己的函数加入入口数组中。

以下定义了 HelloPHP 函数的内容:
    PHP_FUNCTION(HelloPHP)
    {
     php_printf("Hello, PHP v5.2.5 - 2008-3-28");
    }

其实还有个 confirm_myphpext_compiled 函数,是自动产生的,用于测试,与我们的自定义函数用法一模一样。

5,编译、链接,生成最终的文件。

C:\php-5.2.5-Win32\ext>msdev myphpext\myphpext.dsp /MAKE "myphpext - Win32 Release_TS"
    -----------Configuration: myphpext - Win32 Release_TS-----------
    Compiling...
    myphpext.c
    Linking...
       Creating library Release_TS/php_myphpext.lib and object Release_TS/php_myphpext.exp

php_myphpext.dll - 0 error(s), 0 warning(s)

最终在 C:\php-5.2.5-Win32\Release_TS 下生成了扩展库 php_myphpext.dll。

6,部署:

把 php_myphpext.dll 拷贝到 C:\php-5.2.5-Win32\ext 文件夹下。修改 php.ini 文件:

加语句 extension=php_myphpext.dll。

再注意 extension 路径的指向,需要把 ;extension_dir = "./" 语句的注释去掉,再修改为 extension_dir = "C:\php-5.2.5-Win32\ext"。

最后一定要重启 IIS 服务器。

7,测试:

把 myphpext.php 拷贝到 Web 服务器根下(myphpext.php 的代码也值得一看),在本机用浏览器打开:http://localhost/myphpext.php,应该能看到以下信息:

Functions available in the test extension:
    confirm_myphpext_compiled
    HelloPHP

Congratulations! You have successfully modified ext/myphpext/config.m4. Module myphpext is now compiled into PHP.

再建立一个 test.php 文件,内容为:

   <?php
      HelloPHP();
    ?>

php写扩展的更多相关文章

  1. Python之美[从菜鸟到高手]--一步一步动手给Python写扩展(异常处理和引用计数)

    我们将继续一步一步动手给Python写扩展,通过上一篇我们学习了如何写扩展,本篇将介绍一些高级话题,如异常,引用计数问题等.强烈建议先看上一篇,Python之美[从菜鸟到高手]--一步一步动手给Pyt ...

  2. c#写扩展方法

    学习MVC时,学会了写扩展方法,用起来很方便. 01 using System; 02 using System.Collections.Generic; 03 using System.Linq; ...

  3. CAD在网页中绘图,并为新绘的对象写扩展数据和读取扩展数据

    在网页中绘图,并为新绘的对象写扩展数据和读取扩展数据.下面帮助的完整例子,在控件安装目录的 Sample\Ie\iedemo.htm 中. 主要用到函数说明: _DMxDrawX::InsertBlo ...

  4. CAD在网页绘一个直线,得到直线id,再调该得到直线对象,然写扩展数据

    IMxDrawDatabase::ObjectIdToObject 实体id返回实体对象. 参数 说明 [in] LONGLONG lId 实体id JS代码,中绘一个直线,得到直线id,再调该得到直 ...

  5. Flask的session——关于写扩展所学习到的

    这两天端午节.趁着端午节没事干,写了个flask的扩展--flask-RedisSession 在flask中使用该扩展可以让你借助redis数据库轻松获得server-side session. 这 ...

  6. firefox 自写扩展改版,总结

    自己写的扩展,油猴功能,进一步改进,增加了许多操作.原来只是在13以下版本下面能用,主要是在13版本下面chrome代码和page下面代码能够直接互调,13版本以后就不可以了,最近考虑到新版Firef ...

  7. 如何给selenium.chrome写扩展拦截或转发请求

    Selenium.WebDriver Selenium WebDriver 是一组开源 API,用于自动测试 Web 应用程序,利用它可以通过代码来控制chrome浏览器! 有时候我们需要mock接口 ...

  8. PHP扩展开发 第一课 为什么要写扩展及hello world

    PHP扩展开发其实很简单.那为什么要扩展开发呢. 这里咱们以实际的案例进行对比. 第一步,进入 php源码包   http://www.php20.com/forum.php?m ... =159&a ...

  9. 使用C语言给php写扩展

    1.在php源码路径的ext文件夹下,新建一个extend_test.def文件,编辑文件内容为 string my_test_function(string str,int n) 2.在当前目录执行 ...

随机推荐

  1. Task和BackTask

    一.总结性知识点:     1.Android应用运行时会创建任务Task,用于存放主窗口     2.每一个任务包含一个堆栈数据结构,用于保存当前应用已创建的窗口对象,这个堆栈即回退栈BackSta ...

  2. webpack的学习

    什么是webpack? 他有什么优点? 首先对于很多刚接触webpack人来说,肯定会问webpack是什么?它有什么优点?我们为什么要使用它?带着这些问题,我们来总结下如下: Webpack是前端一 ...

  3. yeoman的学习

    官网地址:http://yeoman.io/ 什么是yeoman? 在上一篇博客已粗劣地提到yeoman的安装和验证.说白了,其实yeoman是生成代码和搭建框架的前端自动化工具.为了做到这些,yeo ...

  4. python numpy sum函数用法

    numpy.sum numpy.sum(a, axis=None, dtype=None, out=None, keepdims=False)[source] Sum of array element ...

  5. 纯css实现鼠标感应弹出二级菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. crontab 每月最后一天

    0 8 28-31 * * [ `date -d tomorrow +%e` -eq 1 ] && do-something   我觉得能想到这种方法的,都是经验丰富的人.程序员们,想 ...

  7. 嵌入式 hi3518平台以太网网络模块设计包括重连机制和网线检测机制

    <span style="font-family:Courier New;"> #include <sys/types.h> #include <st ...

  8. Linux共享内存(一)

    inux系统编程我一直看 <GNU/LINUX编程指南>,只是讲的太简单了,通常是书和网络上的资料结合着来掌握才比较全面 .在掌握了书上的内容后,再来都其他资料 . 原文链接 http:/ ...

  9. HDU 4540 威威猫系列故事——打地鼠

    威威猫系列故事--打地鼠 Time Limit: 300/100 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Su ...

  10. CURL --- 命令行浏览器CURL

    CURL --- 命令行浏览器CURL   CURL --- 命令行浏览器   CURL? 嗯,说来话长了~~~~ 这东西现在已经是苹果机上内置的命令行工具之一了,可见其魅力之一斑 1)二话不说,先从 ...