php bin,到網(wǎng)上下載一個php源碼包,解壓,安裝。
php的解壓目錄記為 phpsrc(如:/home/src/php-4.4.4) ,安裝目錄記為 phpbin(如 /usr/local/php) 在shell下輸入(以后遇到有shell的地方我就用#開頭,不另陳述)
# cd phpsrc/ext # ./ext_skel --extname=exthelloword
Creating directory exthelloword Creating basic files: config.m4 .cvsignore exthelloword.c php_exthelloword.h CREDITS EXPERIMENTAL tests/001.phpt exthelloword.php [done].
To use your new extension, you will have to execute the following steps:
1. $ cd .. 2. $ vi ext/exthelloword/config.m4 3. $ ./buildconf 4. $ ./configure --[with|enable]-exthelloword 5. $ make 6. $ ./php -f ext/exthelloword/exthelloword.php 7. $ vi ext/exthelloword/exthelloword.c 8. $ make
Repeat steps 3-6 until you are satisfied with ext/exthelloword/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.
系統(tǒng)自動生成 exthelloword文件夾;接下來我們要修改幾個文件:config.m4, exthelloword.c,php_exthelloword.h, 如下:
1) 修改config.m4
# cd exthelloword # vi config.m4
找到這幾行
dnl PHP_ARG_ENABLE(exthelloword, whether to enable exthelloword support,
dnl Make sure that the comment is aligned: dnl [ --enable-exthelloword Enable exthelloword support]) 去掉這幾行前面的dnl,改為 PHP_ARG_ENABLE(exthelloword, whether to enable exthelloword support,
Make sure that the comment is aligned: [ --enable-exthelloword Enable exthelloword support])
這樣以后編譯php時,./configure后面加 --enable-exthelloword 就可以加載你的php模塊了!
(接下來的2)你可以做也可以不做,直接跳到第4步也可以運(yùn)行。)
2) 修改exthelloword.c,輸出自己想要的東西
# vi exthelloword.c
找到這段 PHP_FUNCTION(confirm_exthelloword_compiled) { char *arg = NULL; int arg_len, len; char string[256];
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) { return; }
len = sprintf(string, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "exthelloword", arg); RETURN_STRINGL(string, len, 1); } 改為: PHP_FUNCTION(confirm_exthelloword_compiled) { zend_printf("This is a exthelloword !"); }
3)編譯鏈接
# cd phpsrc/ext # cc -fpic -DCOMPILE_DL_EXTHELLOWORD=1 -I/usr/local/include -I. -I../main -I.. -I../TSRM -I../Zend -c -o exthelloword/exthelloword.o exthelloword/exthelloword.c
執(zhí)行完之后會在 目錄下生成一個exthelloword.o文件,接下來連接:
# cc -shared -L/usr/local/lib -rdynamic -o exthelloword/exthelloword.so exthelloword/exthelloword.o
4)測試:
有兩種途徑可以測試你的擴(kuò)展模塊是否正確,一是在phpbin目錄下運(yùn)行test.php, 二是在web browser上運(yùn)行test.php(如果已經(jīng)安裝apache等web服務(wù)器的話)。
這里采用phpbin測試,不需要任何web服務(wù)器。
拷貝exthelloword.so到phpbin的相應(yīng)目錄下
(如果不知道是哪個目錄,或者沒有拷貝到正確的位置,在運(yùn)行的時候出錯信息里會指出應(yīng)該在什么路徑。) # mkdir -p phpbin/lib/php/extensions/ (其實這個目錄就是看你php.ini里設(shè)置extension_dir了) # cp exthelloword/exthelloword.so phpbin/lib/php/extensions/
在phpbin目錄下新建一個test.php文件,在里面寫入
<?php
dl("exthelloword.so");
//調(diào)用函數(shù)
exthelloword();
?>
在phpbin目錄下運(yùn)行 執(zhí)行./php –q test.php,如果過程無誤,將會顯示: This is a exthelloword !
測試成功,接下來我們可以往擴(kuò)展模塊中添加自己的函數(shù),實現(xiàn)自己的功能啦。
二. 向擴(kuò)展模塊中添加函數(shù)
向php擴(kuò)展模塊中添加函數(shù),只需要修改exthelloword.c和php_exthelloword.h。
比如要向擴(kuò)展模塊中添加一個函數(shù)sayhello(),我們需要做以下工作:
1) 修改 php_exthelloword.h 添加函數(shù)聲明 在文件中PHP_FUNCTION(confirm_my_module_compiled);一行前面添加下面的代碼 PHP_FUNCTION(say_hello); 保存文件退出
2) 修改 exthelloword.c
在function_entry中添加函數(shù)的entry:
function_entry my_module_functions[] = { PHP_FE(say_hello, NULL) /* ß添加著一行代碼 */ PHP_FE(confirm_my_module_compiled, NULL) /* For testing, remove later. */ {NULL, NULL, NULL} /* Must be the last line in my_module_functions[] */ };
在文件的最后添加函數(shù)的實現(xiàn): PHP_FUNCTION(say_hello) { zend_printf("hello world\n"); } 保存文件退出
3)重新編譯鏈接,生成新的.so文件。
# cd phpsrc/ext # cc -fpic -DCOMPILE_DL_EXTHELLOWORD=1 -I/usr/local/include -I. -I../main -I.. -I../TSRM -I../Zend -c -o exthelloword/exthelloword.o exthelloword/exthelloword.c
執(zhí)行完之后會在 目錄下生成一個exthelloword.o文件,接下來連接:
# cc -shared -L/usr/local/lib -rdynamic -o exthelloword/exthelloword.so exthelloword/exthelloword.o
三. 調(diào)用第三方C/C++庫
在PHP擴(kuò)展模塊中調(diào)用第三方的C/C++庫,與普通的C/C++程序調(diào)用C/C++庫一樣,只要把庫文件的位置放對就可以了。下面舉例說明:
1. 調(diào)用動態(tài)庫a.so
需要的文件,a.so,a.h(a.h主要做數(shù)據(jù)類型聲明和入口函數(shù)聲明),都放在 exthelloword目錄下。
調(diào)用a.so:
1) 在exthelloword.c的開頭添加a.h:
#include "php.h
#include "php_ini.h" #include "a.h" 添加了頭文件后,可以在exthelloword.c的函數(shù)中調(diào)用a.so的函數(shù)接口。
2) 重新編譯鏈接,鏈接時加入a.so:
# cd phpsrc/ext # cc -fpic -DCOMPILE_DL_EXTHELLOWORD=1 -I/usr/local/include -I. -I../main -I.. -I../TSRM -I../Zend -c -o exthelloword/exthelloword.o exthelloword/exthelloword.c
執(zhí)行完之后會在 目錄下生成一個exthelloword.o文件,接下來連接:
# cc -shared -L/usr/local/lib -rdynamic -o exthelloword/exthelloword.so exthelloword/exthelloword.o exthelloword/a.so
[如果a.so內(nèi)部實現(xiàn)是C++,鏈接時還應(yīng)該加入?yún)?shù) –lstdc++,即:
# cc –shared –lstdc++ -L/usr/local/lib -rdynamic -o exthelloword/exthelloword.so exthelloword/exthelloword.o exthelloword/a.so
]
3) 測試exthelloword.so時,把exthelloword.so和a.so都拷貝到phpbin/lib/php/extensions/下。
2. 調(diào)用靜態(tài)庫a.a
需要的文件,a.so,a.h(a.h主要做數(shù)據(jù)類型聲明和入口函數(shù)聲明),都放在 exthelloword目錄下。
調(diào)用a.a:
1) 和2)都與調(diào)用a.so相同。
3) 測試exthelloword.so時,把exthelloword.so拷貝到phpbin/lib/php/extensions/下,a.a不需要。
|