搜尋
首頁後端開發php教程 VC 建立php扩张

VC 建立php扩展

软件 php的源文件和安装包要一致

php5.3.8(VC9 x86 Thread Safe)
php5.3.8源文件(tar.bz2)
VC

bison.exe

MSYS(MSYS类似于Cygwin,但是由于工作原理的不同,速度更快、体积更小、功能强大、便于携带http://code.google.com/p/msys-cn/)

因为我的开发只是一个很简单的demo,没有使用第三方了类库。如果是把linux下拿过来的扩展项目,可能用到一些库。可能用cygwin会比较好。但是没有cygwin完全可以在window下开发。

1。解压下载到的源文件包tar.bz2包到C盘c:/phpsrc,并且解压php安装包(VC9 x86Thread Safe,也就是能够正常使用的php压缩包文件)到C:/php,我们只需要里面的一个文件C:/php/dev/ php5.lib,复制php5.lib到c:/phpsrc。

2。复制bison.exe到Microsoft Visual Studio\Common\MSDev98\Bin把Microsoft Visual Studio\Common\MSDev98\Bin的绝对路径添加到windows环境变量

3 .在这里我们开始生成生成config.w32.h。CMD在里面操作

进入:c:/phpsrc执行

C:\phpsrc>buildconf
Rebuilding configure.js
Now run 'configure --help'

?建立一个临时环境变量

C:\phpsrc>set path=%path%;C:/phpsrc/bin

C:\phpsrc>cscript /nologo configure.js --with-php-build="../phpsrc" --without-libxml --disable-odbc

如果想要Non Thread Safe 模式就去掉上面的命令最后的参数--disable-zts

Saving configure options to config.nice.bat
Checking for cl.exe ...  <in default path>
  Detected compiler MSVC9 (Visual C++ 2008)
  Detected 32-bit compiler
Checking for link.exe ...  C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN
Checking for nmake.exe ...  <in default path>
Checking for lib.exe ...  <in default path>
...........中间省略.............
-------------------------------------------
|               |                         |
-------------------------------------------
| Build type    | Release                 |
| Thread Safety | Yes                     |
| Compiler      | MSVC6 (Visual C++ 6.0)  |
| Architecture  | x86                     |
-------------------------------------------

Type 'nmake' to build PHP</in></in></in>

如果出现上类似的提示, 说明你的PHP开发环境已经搭建成功,同时在main下面多了一个 config.w32.h。即PHP开发环境的配置文件。如果没有这个脚本,windows下开发php,简直太悲惨了。

?

4.开发PHP扩展的方法

到这里我们就可以建立开发PHP扩展了,但是无从下手,我们该怎么办,其实PHP也给我们提供了很好用的工具来建立一个PHP扩展的骨架。就是用C:\phpsrc\ext下的ext_skel_win32.php如何运行,以php结尾,说明依赖php。下面看具体方法,我建立一个名为’test‘的扩展。这里要确保你的php.exe可以直接在cmd下使用,具体还是加入Path路径 。虽然有ext_skel_win32.php,用php执行,但是我们不知道具体的内容。 其实执行如下命令:

C:\phpsrc\ext>php ext_skel_win32.php
'sh' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

ext_skel_win32.php源码中说要使用cygwin,但我机器上没有装cygwin,另外发现其中实际上只使用到了sh,而我机器上装的MSYS里也有sh,应该可以用的吧,于是就将ext_skel_win32.php中的$cygwin_path变量设置成了MSYS的BIN目录

$cygwin_path = 'c:\msys\1.0\bin';
?
C:\phpsrc\ext>php ext_skel_win32.php
ext_skel --extname=module [--proto=file] [--stubs=file] [--xml[=file]]
           [--skel=dir] [--full-xml] [--no-help]

  --extname=module   module is the name of your extension
  --proto=file       file contains prototypes of functions to create
  --stubs=file       generate only function stubs in file
  --xml              generate xml documentation to be added to phpdoc-cvs
  --skel=dir         path to the skeleton directory
  --full-xml         generate xml documentation for a self-contained extension
                     (not yet implemented)
  --no-help          don't try to be nice and create comments in the code
                     and helper functions to test if the module compiled

PHP Warning:  fopen(/.php): failed to open stream: No such file or directory in
C:\phpsrc\ext\ext_skel_win32.php on line 52

C:\phpsrc\ext>

?可以看到如何使用这php脚本。大体命令如下

c:\phpsrc\ext>php ext_skel_win32.php  --extname=test
Creating directory test
Creating basic files: config.m4 config.w32 .svnignore test.c php_test.h CREDITS EXPERIMENTAL tests/001.phpt test.php [do
ne].

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

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

Repeat steps 3-6 until you are satisfied with ext/test/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.

c:\phpsrc\ext>

这样就成功创建了一个php扩展骨架 ,你可以在里面修改。具体的还要好好研究PHP API。具体扩展的位置就在ext目录下,打开可以看到test文件夹,这就是刚才命令创建的。

C:\phpsrc\ext\test 的目录

2011-11-29  16:57    <dir>          .
2011-11-29  16:57    <dir>          ..
2011-11-29  16:57                16 .svnignore
2011-11-29  16:57             1,970 config.m4
2011-11-29  16:57               282 config.w32
2011-11-29  16:57                 4 CREDITS
2011-11-29  16:57                 0 EXPERIMENTAL
2011-11-29  16:57             2,768 php_test.h
2011-11-29  16:57             5,128 test.c
2011-11-29  16:57             4,954 test.dsp
2011-11-29  16:57               500 test.php
2011-11-29  16:57    <dir>          tests
               9 个文件         15,622 字节
               3 个目录  7,441,883,136 可用字节

C:\phpsrc\ext\test></dir>
</dir>
</dir>

这样一个php扩展的框架已经创建完成了。

?

下面就是配置使用vc++6开发这个扩展

III. 添加依赖的php5ts.lib
? 在php的二进制包中的 dev目录下将 php5ts.lib 拷到我们的test目录中, 否则编译将通不过。
IV. 添加test c代码
生成的test目录中有关键文件包括
? test.dsp,
? test.c,
? php_test.h,
其他文件暂时不必关心.

提示:切忌test目录不可以挪移出ext目录,否则会编译报缺少php.h.

1. 修改php_test.h
扩展的新函数: 在PHP_FUNCTION(confirm_test_compiled); 行后添加一行

PHP_FUNCTION(confirm_test_compiled);
PHP_FUNCTION(test);  // 新增的行

2. 修改test.c
在PHP_FUNCTION(confirm_test_compiled) 后添加我们的新函数

PHP_FUNCTION(test){
    php_printf("Hello C extension");
}

在数组zend_function_entry test_functions[]增加一行

const zend_function_entry test_functions[] = {
	PHP_FE(confirm_test_compiled,	NULL)		/* For testing, remove later. */
	PHP_FE(test, NULL) // 新增的行
	PHP_FE_END	/* Must be the last line in test_functions[] */
};

?V. 构建DLL文件

用vc6打开我们的工程,就是test.dsp
1. 修改编译方式为release: 选择Build->Set Active Configuration设置默认编译方式为Release, 否则会提示缺少php5ts_debug.lib ,其实就是php5ts.lib。
2. 按F5编译。会在ext上级的Release_TS目录下生成php_test.dll

提示:如果愿意使用命令行编译也是可以的,命令如下:

msdev test\test.dsp /MAKE "test - Win32 Release_TS" 

?VI. 集成dll到php中。

?

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
PHP電子郵件:分步發送指南PHP電子郵件:分步發送指南May 09, 2025 am 12:14 AM

phpisusedforsendendemailsduetoitsignegrationwithservermailservicesand andexternalsmtpproviders,自動化intifications andMarketingCampaigns.1)設置設置yourphpenvenvironnvironnvironmentwithaweberswithawebserverserververandphp,確保themailfunctionisenabled.2)useabasicscruct

如何通過PHP發送電子郵件:示例和代碼如何通過PHP發送電子郵件:示例和代碼May 09, 2025 am 12:13 AM

發送電子郵件的最佳方法是使用PHPMailer庫。 1)使用mail()函數簡單但不可靠,可能導致郵件進入垃圾郵件或無法送達。 2)PHPMailer提供更好的控制和可靠性,支持HTML郵件、附件和SMTP認證。 3)確保正確配置SMTP設置並使用加密(如STARTTLS或SSL/TLS)以增強安全性。 4)對於大量郵件,考慮使用郵件隊列系統來優化性能。

高級PHP電子郵件:自定義標題和功能高級PHP電子郵件:自定義標題和功能May 09, 2025 am 12:13 AM

CustomHeadersheadersandAdvancedFeaturesInphpeMailenHanceFunctionalityAndreliability.1)CustomHeadersheadersheadersaddmetadatatatatataatafortrackingandCategorization.2)htmlemailsallowformattingandttinganditive.3)attachmentscanmentscanmentscanbesmentscanbestmentscanbesentscanbesentingslibrarieslibrarieslibrariesliblarikelikephpmailer.4)smtppapapairatienticationaltication enterticationallimpr

使用PHP和SMTP發送電子郵件的指南使用PHP和SMTP發送電子郵件的指南May 09, 2025 am 12:06 AM

使用PHP和SMTP發送郵件可以通過PHPMailer庫實現。 1)安裝並配置PHPMailer,2)設置SMTP服務器細節,3)定義郵件內容,4)發送郵件並處理錯誤。使用此方法可以確保郵件的可靠性和安全性。

使用PHP發送電子郵件的最佳方法是什麼?使用PHP發送電子郵件的最佳方法是什麼?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

PHP中依賴注入的最佳實踐PHP中依賴注入的最佳實踐May 08, 2025 am 12:21 AM

使用依賴注入(DI)的原因是它促進了代碼的松耦合、可測試性和可維護性。 1)使用構造函數注入依賴,2)避免使用服務定位器,3)利用依賴注入容器管理依賴,4)通過注入依賴提高測試性,5)避免過度注入依賴,6)考慮DI對性能的影響。

PHP性能調整技巧和技巧PHP性能調整技巧和技巧May 08, 2025 am 12:20 AM

phpperformancetuningiscialbecapeitenhancesspeedandeffice,whatevitalforwebapplications.1)cachingwithapcureduccureducesdatabaseloadprovesrovessetimes.2)優化

PHP電子郵件安全性:發送電子郵件的最佳實踐PHP電子郵件安全性:發送電子郵件的最佳實踐May 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境