用C語言,php的擴展的書寫格式(ZEND API)寫PHP擴展的步驟:
我用的php環境 php 5.2.5,完成最基本功能 helloword

cd /php5.2.5/ext

生成骨架 ./ext_skel --extname=cltest 會在當前目錄建立一個cltest的目錄

進入該目錄 cd cltest

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

注釋3個dnl [dnl代表注釋,對應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) /* ?添加這一行代碼   注冊函數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);
}

修改完畢。

 

vi php_cltest.h

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

修改完畢。

 

找到這個執行文件phpize ,在cltest目錄下執行命令,用于加載動態鏈接庫

/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
會在當前的目錄下生成一個目錄叫modules他的下面就放著你要的cltest.so文件

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