LINUX下PHP擴充模組的開發與測試(原創)
作者:餘超 Email:yuchao86@gmail.com
關於php的擴充模組開發,很多人都很害怕,我在新浪工作兩年到現在的模組開發靈活運用,特發此文章供大家參考
首先確保你的開發環境配置正確,
我的如下:
[yuchao@yuchao-Latitude-E5410 branches_1]$env |grep PATH
CPLUS_INCLUDE_PATH=/usr/include:/usr/include/c++/4.5:/usr/share/ada/adainclude/rts-sjlj/adainclude:
LIBRARY_PATH=/usr/local/lib:/usr/share/ada/adainclude/rts-sjlj/adalib:
PATH=/home/yuchao/dev/hadoop/bin:/usr/local/jdk1.7.0/bin:/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
C_INCLUDE_PATH=/usr/local/include:/usr/include/c++/4.5:/usr/share/ada/adainclude/rts-sjlj/adainclude:
[yuchao@yuchao-Latitude-E5410 branches_1]$gcc --version
gcc (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
等等一切工具
然後產生一個簡單的php extension
我們需要兩個目錄:/home/yuchao/source/php-5.3.6/ext,到網上下載一個php源碼包,解壓,安裝。
php的解壓目錄記為 source(如:/home/yuchao/source/php-5.3.6/ext) ,安裝目錄記為 /usr/bin(如 /usr/local/php)
在shell下輸入(以後遇到有shell的地方我就用#開頭,不另陳述)
# cd phpsrc/ext
# ./ext_skel --extname=yuchao
Creating directory yuchao
Creating basic files: config.m4 config.w32 .svnignore yuchao.c php_yuchao.h CREDITS EXPERIMENTAL tests/001.phpt yuchao.php [done].
To use your new extension, you will have to execute the following steps:
1. $ cd ..
2. $ vi ext/yuchao/config.m4
3. $ ./buildconf
4. $ ./configure --[with|enable]-yuchao
5. $ make
6. $ ./php -f ext/yuchao/yuchao.php
7. $ vi ext/yuchao/yuchao.c
8. $ make
Repeat steps 3-6 until you are satisfied with ext/yuchao/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.
系統自動產生yuchao檔案夾;接下來我們要修改幾個檔案:config.m4, yuchao.c,php_yuchao.h, 如下:
1) 修改config.m4
# cd yuchao
# vi config.m4
找到這幾行
dnl PHP_ARG_ENABLE(yuchao, whether to enable yuchao support,
dnl Make sure that the comment is aligned:
dnl [ --enable-yuchao Enable yuchao support])
去掉這幾行前面的dnl,改為
PHP_ARG_ENABLE(yuchao, whether to enable yuchao support,
Make sure that the comment is aligned:
[ --enable-yuchao Enable yuchao support])
這樣以後編譯php時,./configure後面加 --enable-yuchao 就可以載入你的php模組了!
(接下來的2)你可以做也可以不做,直接跳到第4步也可以運行。)
2) 修改yuchao.c,輸出自己想要的東西
# vi yuchao.c
找到這段
PHP_FUNCTION(confirm_test_yuchao)
{
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.", "yuchao", arg);
RETURN_STRINGL(string, len, 1);
}
改為:
PHP_FUNCTION(confirm_test_yuchao)
{
zend_printf("This is yuchao module !");
}
3)編譯連結
# cd ../ext
# sudo cc -fpic -DCOMPILE_DL_TEST_MODULE=1 -I/usr/local/include -I. -I../main -I.. -I../TSRM -I../Zend -c -o yuchao/yuchao.o yuchao/yuchao.c
執行完之後會在 目錄下產生一個test_module.o檔案,接下來串連:
# sudo cc -shared -L/usr/local/lib -rdynamic -o yuchao/yuchao.so yuchao/yuchao.o
4)測試:
有兩種途徑可以測試你的擴充模組是否正確,一是在PATH目錄下運行test.php, 二是在web browser上運行test.php(如果已經安裝apache等web伺服器的話)。
這裡採用PATH測試,不需要任何web伺服器。
拷貝test_module.so到PATH的相應目錄下
(如果不知道是哪個目錄,php.ini裡設定extension_dir裡會指出應該在什麼路徑。)
# mkdir -p /lib/php/extensions/
# cp yuchao/yuchao.so /lib/php/extensions/
在PATH目錄下建立一個test.php檔案,在裡面寫入
dl("yuchao.so");
//調用函數
yuchao();
?>
在PATH目錄下運行
執行./php –q test.php,如果過程無誤,將會顯示:
This is yuchao module !
測試成功,接下來我們可以往擴充模組中添加自己的函數,實現自己的功能
下一篇將會介紹函數的功能實現,如果你等不急了,
就請自己看PHP ext目錄下面的原始碼吧。