天天看點

Windows下的PHP擴充程式設計

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:           |

      | 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          |

      | 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”

    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.

    #define MYPHPEXT_G(v) TSRMG(myphpext_globals_id, zend_myphpext_globals *, v)

    #define MYPHPEXT_G(v) (myphpext_globals.v)

    #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 檔案:

    #ifdef HAVE_CONFIG_H

    #include “config.h”

    #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,

     ”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),

     ”0.1″, /* Replace with version number for your extension */

     STANDARD_MODULE_PROPERTIES

    #ifdef COMPILE_DL_MYPHPEXT

    ZEND_GET_MODULE(myphpext)

    /* {{{ 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();

    /* Remove if there’s nothing to do at request start */

    /* {{{ PHP_RINIT_FUNCTION

    PHP_RINIT_FUNCTION(myphpext)

    /* Remove if there’s nothing to do at request end */

    /* {{{ PHP_RSHUTDOWN_FUNCTION

    PHP_RSHUTDOWN_FUNCTION(myphpext)

    /* {{{ 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.

    注意本例定義了一個函數 HelloPHP。在 php_myphpext.h 檔案中定義:

    在 myphpext.c 中有 2 處地方:

    PHP_FE(HelloPHP, NULL) 語句把我們自己的函數加入入口數組中。

    以下定義了 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,測試:

    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();

    ?>

    Hello, PHP v5.2.5 - 2008-3-28

    這說明我們所有的步驟都是正确的,已經生成了一個自己的 PHP 擴充函數庫。隻要對 C 語言熟悉,就可以編寫大量的自定義函數,供所有人調用。注意,不像 PEAR 等函數庫,需要首先在 PHP 代碼裡指定其檔案名才能使用其中的函數和類。PHP 擴充的函數不是用 PHP 語言自身開發的,而是 C 開發的,而且可以直接在 PHP 代碼裡調用。這樣既有一定的保密性,還有效率上的優勢。

    開發 PHP 擴充的更多示例可以參考 PHP 的源代碼,或者其它的 PECL 源代碼。從那裡可以學習到大量的技巧。

本文轉自網眼51CTO部落格,原文連結:http://blog.51cto.com/itwatch/286489,如需轉載請自行聯系原作者