LINUX下PHP擴展模塊的開發和測試(原創)

LINUX下PHP擴展模塊的開發和測試(原創) 
作者:餘超 Email:[email protected]
關於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目錄下面的源代碼吧。


發佈了46 篇原創文章 · 獲贊 5 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章