用C開發PHP擴充 執行個體(基礎版)_PHP教程

來源:互聯網
上載者:User
第一步:建立擴充骨架。
[html]
cd /usr/local/src/php-5.3.6/ext/
./ext_skel --extname=laiwenhui

cd /usr/local/src/php-5.3.6/ext/
./ext_skel --extname=laiwenhui第二步:修改編譯參數。
[html] view plaincopyprint?cd php-5.3.6/ext/laiwenhui
vi config.m4

cd php-5.3.6/ext/laiwenhui
vi config.m4去掉
PHP_ARG_ENABLE(laiwenhui, whether to enable laiwenhui support,
[ --enable-laiwenhui Enable laiwenhui support])
兩行前面的dnl
修改後為:
dnl Otherwise use enable:
PHP_ARG_ENABLE(laiwenhui, whether to enable laiwenhui support,
dnl Make sure that the comment is aligned:
[ --enable-laiwenhui Enable laiwenhui support])


第三步:編寫代碼
[html]
vim php_laiwenhui.h

vim php_laiwenhui.h

PHP_FUNCTION(confirm_laiwenhui_compiled);

後面新增一行

PHP_FUNCTION(test);

[html]
vim laiwenhui.c

vim laiwenhui.c

PHP_FE(confirm_laiwenhui_compiled, NULL)

後面添加
PHP_FE(test, NULL)


**在檔案最後面增加如下代碼:**
[html]
PHP_FUNCTION(test)
{
char *arg = "This is my first extention!";
int len;
char *strg;

len = spprintf(&strg, 0, "%s\n", arg);
RETURN_STRINGL(strg, len, 0);
}

PHP_FUNCTION(test)
{
char *arg = "This is my first extention!";
int len;
char *strg;

len = spprintf(&strg, 0, "%s\n", arg);
RETURN_STRINGL(strg, len, 0);
}第四步:編譯代碼
[html]
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install

/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
我的PHP安裝路徑為:/usr/local/php
這個時候會產生一個檔案 /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/laiwenhui.so

編輯PHP設定檔php.ini,添加擴充:
[html]
vim php.ini

vim php.ini在[PHP]模組下增加:
[html]
extension = laiwenhui.so

extension = laiwenhui.so把php.ini 檔案中的 extension_dir 修改為該目錄:
[html]
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"

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


第五步:檢查安裝結果

1.重啟php-fpm,ngixn
2./use/local/php/bin/php -m
看下是否有包含laiwenhui擴充。


第六步:執行測試代碼


在網站根目錄建立test.php
vim test.php
代碼內容如下
echo test();

執行後結果為:
This is my first extention!

如果你能順利完成以上幾步,恭喜你完成了第一個擴充。

http://www.bkjia.com/PHPjc/477166.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477166.htmlTechArticle第一步:建立擴充骨架。 [html] cd /usr/local/src/php-5.3.6/ext/ ./ext_skel --extname=laiwenhui cd /usr/local/src/php-5.3.6/ext/ ./ext_skel --extname=laiwenhui第二步:修...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.