初步介紹PHP擴充開發經驗分享

來源:互聯網
上載者:User

環境:PHP 5.2.14 CentOS 5.5

第一步:建立擴充骨架

cd php-5.2.14/ext
./ext_skel –extname=laiwenhui

第二步:修改編譯參數

cd php-5.2.14/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])

第三步:編寫代碼

vim php_laiwenhui.h

在 PHP_FUNCTION(confirm_laiwenhui_compiled); 後面新增一行:PHP_FUNCTION(test);

添加後為:

PHP_FUNCTION(confirm_laiwenhui_compiled); /* For testing, remove later. */
PHP_FUNCTION(test);

然後

vim laiwenhui.c

在PHP_FE(confirm_laiwenhui_compiled, NULL) 後面添加 PHP_FE(test, NULL)

添加後為: 複製代碼 代碼如下:zend_function_entry laiwenhui_functions[] = {
PHP_FE(confirm_laiwenhui_compiled, NULL) /* For testing, remove later. */
PHP_FE(test, NULL) /* For testing, remove later. */
{NULL, NULL, NULL} /* Must be the last line in laiwenhui_functions[] */
};

在檔案最後面增加如下代碼: 複製代碼 代碼如下:PHP_FUNCTION(test)
{
char *arg = “This my first extention!”;
int len;
char *strg;
len = spprintf(&strg, 0, “%s\n”, arg);
RETURN_STRINGL(strg, len, 0);
}

第四步:編譯代碼
複製代碼 代碼如下:cd php-5.2.6/ext/laiwenhui
/opt/module/php/bin/phpize
./configure –with-php-config=/opt/module/php/bin/php-config
make
make install

我的PHP安裝路徑為:/opt/module/php
這個時候會產生一個檔案 /opt/module/php/lib/php/extensions/no-debug-non-zts-20060613/laiwenhui.so

編輯PHP設定檔php.ini,添加擴充:

vim php.ini

在[PHP]模組下增加:extension = laiwenhui.so

;extension=php_zip.dll
extension = laiwenhui.so

把php.ini 檔案中的 extension_dir 修改為該目錄:
extension_dir = “/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/”

; Directory in which the loadable extensions (modules) reside.
extension_dir = “/opt/module/php/lib/php/extensions/no-debug-non-zts-20060613/”

第五步:檢查安裝結果

1. 重啟apache或者php-fpm
2. /opt/module/php/bin/php -m 看下是否有包含laiwenhui擴充。
第六步:執行測試代碼

在網站根目錄建立test.php

vim test.php

代碼內容如下

<?php
echo test();
?>

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

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

以上是擴充PHP的簡單步驟,更多請參考:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.