快速開發乙個PHP擴充套件

2021-05-09 22:20:56 字數 2350 閱讀 1535

快速開發乙個php擴充套件

本文通過非常快速的方式講解了如何製作乙個php 5.2 環境的擴充套件(php extension),希望能夠在**的方式下讓想快速學習的朋友了解一下製作過程。

步驟一:生成擴充套件框架

cd /root/soft/php/php-5.2.6/ext

./ext_skel --extname=heiyeluren

cd /root/soft/php/php-5.2.6/ext/heiyeluren

vi config.m4

開啟檔案後去掉 dnl ,獲得下面的資訊:

php_arg_enable(heiyeluren, whether to enable heiyeluren support,

[  --enable-heiyeluren           enable heiyeluren support])

儲存退出.

(圖01)

第二步:編寫**

vi php_heiyeluren.h

找到:php_function(confirm_heiyeluren_compiled); ,新增一行:

php_function(heiyeluren_test);

儲存退出。

(圖02)

vi heiyeluren.c

陣列裡增加我們的函式,找到 zend_function_entry heiyeluren_functions,增加:

php_fe(heiyeluren, null)

(圖03)

再到 heiyeluren.c 檔案最後面增加如下**:

php_function(heiyeluren_test)

len = spprintf(&strg, 0, "your input string: %s/n", arg);

return_stringl(strg, len, 0);

}儲存退出。

(圖04)

第三步:編譯安裝

cd /root/soft/php/php-5.2.6/ext/heiyeluren

/usr/local/php/bin/phpize

./configure --with-php-config=/usr/local/php/bin/php-config

make

make test

make install

現在看看是不是有個 /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/heiyeluren.so

編輯php.ini,把擴充套件加入進去:

vi /usr/local/php/lib/php.ini

在[php]模組下增加:

extension = heiyeluren.so

儲存退出。

(圖05)

注意:如果你不存在擴充套件檔案目錄,或者安裝報錯,那麼可以自行建立這個目錄,然後把擴充套件拷貝到目錄下,然後記得把 php.ini 檔案中的 extension_dir 修改為該目錄:

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"

(圖06)

第四步:檢查安裝結果

現在看看模組載入了沒有:

/usr/local/php/bin/php -m,應該會列印出:

[php modules]

...heiyeluren

...[zend modules]

然後重啟apache,輸出 phpinfo() ,應該能夠看到:

heiyeluren

heiyeluren support enabled

(圖07)

看看函式是否存在並且呼叫,在web目錄下建立:heiyeluren.php

<?php

echo "

";

print_r(get_loaded_extensions());

print_r(get_extension_funcs('heiyeluren'));

echo heiyeluren_test('my first php extension');

echo "

";

?>

訪問apache,應該能夠看到:

array

(...

[33] => heiyeluren

)array

([0] => confirm_heiyeluren_compiled

[1] => heiyeluren_test

)your input string: heiyeluren

(圖08)

擴充套件製作成功!

快速開發乙個PHP擴充套件

快速開發乙個php擴充套件 本文通過非常快速的方式講解了如何製作乙個php 5.2 環境的擴充套件 php extension 希望能夠在 的方式下讓想快速學習的朋友了解一下製作過程。步驟一 生成擴充套件框架 cd root soft php php 5.2.6 ext ext skel extna...

開發乙個小的php擴充套件

今天試了一下在php新增擴充套件,看了挺多資料,細節上不一致,其他大體是差不多的。我們來開發乙個叫ccvita string的函式,他的主要作用是返回一段字元,對應的php 可能如此 function ccvita string str 即生成鏈結 第一步,生成 進入php源 中自帶的工具ext s...

PHP 拓展 開發,PHP擴充套件開發 第乙個擴充套件

我們先假設業務場景,是需要有這麼乙個擴充套件,提供乙個叫ccvita string的函式,他的主要作用是返回一段字元。這個業務場景實在太假,大家就這麼看看吧 對應的php 可能是這樣 function ccvita string str result link return result 第一步,生...