用C語(yǔ)言,php的擴(kuò)展的書(shū)寫(xiě)格式(ZEND API)寫(xiě)PHP擴(kuò)展的步驟:
我用的php環(huán)境 php 5.2.5,完成最基本功能 helloword

cd /php5.2.5/ext

生成骨架 ./ext_skel --extname=cltest 會(huì)在當(dāng)前目錄建立一個(gè)cltest的目錄

進(jìn)入該目錄 cd cltest

修改 配置文件config.m4
vi cltest/config.m4 

注釋3個(gè)dnl [dnl代表注釋,對(duì)應(yīng)shell的#]
PHP_ARG_WITH(my_module, for my_module support,
Make sure that the comment is aligned:
[ --with-my_module Include my_module support])

修改完畢。

vi cltest.c
改成這樣:
function_entry my_module_functions[] = {
PHP_FE(say_hello, NULL) /* ?添加這一行代碼   注冊(cè)函數(shù)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[] */
};

另外在文件的最后添加下列代碼 函數(shù)程序主體
PHP_FUNCTION(say_hello)
{
        RETURN_STRINGL("hello world",100,1);
}

修改完畢。

 

vi php_cltest.h

在文件中PHP_FUNCTION(confirm_my_module_compiled);一行前面添加下面的代碼 函數(shù)定義
PHP_FUNCTION(say_hello);

修改完畢。

 

找到這個(gè)執(zhí)行文件phpize ,在cltest目錄下執(zhí)行命令,用于加載動(dòng)態(tài)鏈接庫(kù)

/usr/local/php/bin/phpize
./configure --enable-cltest --with-apxs2=/usr/local/apache2/bin/apxs --with-php-config=/usr/local/php/bin/php-config

make
會(huì)在當(dāng)前的目錄下生成一個(gè)目錄叫modules他的下面就放著你要的cltest.so文件

make install
會(huì)cp modules/cltest.so /usr/local/php/include/php/ext/
其余的就是修改 php.ini加載該.so webserver要生效需要重啟。