用C語言,php的擴展的書寫格式(ZEND API)寫PHP擴展的步驟:
到PHP的安裝目錄下 [root@test1 ext]# cd /root/php/php5.2/ext [root@test1 ext]# ./ext_skel --extname=cltest 修改 配置文件config.m4 [root@test1 ext]# vi cltest/config.m4 刪除 3 個 dnl dnl PHP_ARG_WITH(my_module, for my_module support, dnl Make sure that the comment is aligned: dnl [ --with-my_module Include my_module support]) 或者刪除 下邊這3個 dnl dnl PHP_ARG_ENABLE(my_module, whether to enable my_module support, dnl Make sure that the comment is aligned: dnl [ --enable-my_module Enable my_module support]) 修改完畢。 在my_module.c擴展模塊的主程序文件中加PHP的函數定義 主程序中描述了php擴展模塊的聲明,模塊中含有多少個函數,各個函數的作用,在phpinfo函數中顯示什么內容,模塊初始化做些什么,結束做 些什么都會在這個文件里進行描述。我們在上面只是添加了一個函數say_hello,并且描述了say_hello函數的具體內容 [root@test1 ext]# Vi cltest.c function_entry my_module_functions[] = { PHP_FE(say_hello, NULL) /* ?添加這一行代碼 注冊函數say_hello() */ PHP_FE(confirm_my_module_compiled, NULL) /* For testing, remove later. */ {NULL, NULL, NULL} /* Must be the last line in my_module_functions[] */ }; 在文件的最后添加下列代碼 函數程序主體 PHP_FUNCTION(say_hello) { RETURN_STRINGL("hello world",100,1); } 修改完畢。 在my_module.h擴展模塊的頭文件中加PHP啟動時要注冊的函數 在對應的頭文件中聲明了say_hello這個函數,從而完成了我們預期的功能。 [root@test1 ext]# vi php_cltest.h 在文件中PHP_FUNCTION(confirm_my_module_compiled);一行前面添加下面的代碼 函數定義 PHP_FUNCTION(say_hello); 保存文件退出,修改完畢。 找到這個執(zhí)行文件phpize ,在cltest目錄下執(zhí)行命令,用于加載動態(tài)鏈接庫 [root@test1 cltest]# /usr/local/php/bin/phpize 注意apxs的版本 ,使用 --with-apxs 或者 --with-apxs2 命令 [root@test1 cltest]# find / -name apxs -print 執(zhí)行編譯命令 [root@test1 cltest]# ./configure --enable-cltest --with-apxs=/usr/local/apache/bin/apxs --with-php-config=/usr/local/php/bin/php-config //下邊的這部分是 想調用其他的C的動態(tài)鏈接庫的 ,還沒有測試成功。 php_test.so 復制到、/usr/lib/ #include "php_test.h" 直接使用php_test.so提供的接口函數strToupper()。 [root@test1 cltest]# ./configure --enable-cltest LDFLAGS=-lphp_test --with-apxs=/usr/local/apache/bin/apxs --with-php-config=/usr/local/php6/bin/php-config 在php的這個擴展庫的Makefile里面的EXTRA_LIBS加上這個libtest.a //上邊的這部分是 想調用其他的C的動態(tài)鏈接庫的 ,還沒有測試成功。 [root@test1 cltest]# make 會在當前的目錄下生成一個目錄叫modules他的下面就放著你要的cltest.so文件 [root@test1 cltest]# cp modules/cltest.so /usr/local/php/include/php/ext/ 這里需要你先設置你的php的擴展目錄的 在php.ini里面查找“extension_dir” 找到php的執(zhí)行文件 /usr/local/php/bin/php [root@test1 ext]# find / -name php -print [root@test1 ext]# ./php -f /root/php/php5.2/ext/cltest/cltest.php // 測試是否 已經加載成功 在php.ini文件中打開這個擴展/usr/local/php/lib/php.ini extension=jinzhesheng_module.so 然后 重新起動apache sudo /usr/sbin/apachectl stop sudo /usr/sbin/apachectl start [root@test1 ext]# find / -name apachectl -print [root@test1 ext]# /apachectlPATH/apachectl stop [root@test1 ext]# /apachectlPATH/apachectl start 用phpinfo來察看一下 以后只要修改cltest.c文件,在執(zhí)行make命令,重新啟動APACHE 就可以更新 動態(tài)鏈接庫 例如:增加函數chenlong [root@test1 cltest]# vi ext/cltest/cltest.c 增加:PHP_FE(chenlong, NULL) PHP_FUNCTION(chenlong) { //帶參數 zval **yourname; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &yourname) == FAILURE) { WRONG_PARAM_COUNT; } zend_printf("hello world, %s\n", Z_STRVAL_PP(yourname)); } [root@test1 cltest]# vi php_cltest.h 增加:PHP_FUNCTION(chenlong); [root@test1 cltest]# make [root@test1 cltest]# cp modules/ctest.so /usr/local/php/include/php/ext/ [root@test1 ext]# /apachectlPATH/apachectl stop [root@test1 ext]# /apachectlPATH/apachectl start 測試: <? Say_hello(); chenlong("DragonChen"); ?> Makefile文件中: EXTRA_CFLAGS=-I/usr/local/bind/include EXTRA_LDFLAGS=-L/usr/local/bind/lib EXTRA_LIBS=-lbind cp modules/ctest.so /usr/local/php/include/php/ext/ /usr/sbin/apachectl stop /usr/sbin/apachectl start |
|