PHP擴充開發-LINUX環境的代碼執行個體分享

來源:互聯網
上載者:User


LINUX環境下開發PHP擴充的步驟如下:

1、下載PHP源碼,解壓,我的解壓目錄是:/root/lamp/php-5.5.37

2、cd到/root/lamp/php-5.5.37/ext目錄下,建立檔案test_extension.def檔案

int a(int x, int y)string b(string str, int n)

3、通過擴充架構產生器產生架構目錄:
ext_skel –extname=test_extension –proto=test_extension.def
產生成功結果如下:

Creating directory test_extensionawk: /root/lamp/php-5.5.37/ext/skeleton/create_stubs:56: warning: escape sequence `\|' treated as plain `|'Creating basic files: config.m4 config.w32 .svnignore test_extension.c php_test_extension.h CREDITS EXPERIMENTAL tests/001.phpt test_extension.php [done].To use your new extension, you will have to execute the following steps:1.  $ cd ..2.  $ vi ext/test_extension/config.m43.  $ ./buildconf4.  $ ./configure --[with|enable]-test_extension5.  $ make6.  $ ./sapi/cli/php -f ext/test_extension/test_extension.php7.  $ vi ext/test_extension/test_extension.c8.  $ makeRepeat steps 3-6 until you are satisfied with ext/test_extension/config.m4 andstep 6 confirms that your module is compiled into PHP. Then, start writingcode and repeat the last two steps as often as necessary.

4、切換到產生的架構目錄下:cd test_extension
5、修改設定檔config.m4,去掉10、11、12行前面的dnl,如下

PHP_ARG_WITH(test_extension, for test_extension support,Make sure that the comment is aligned:[  --with-test_extension             Include test_extension support])

6、實現函數a和b的功能,vi test_extension.c,修改後函數a、b如下

PHP_FUNCTION(a){        int argc = ZEND_NUM_ARGS();        long x;        long y;        if (zend_parse_parameters(argc TSRMLS_CC, "ll", &x, &y) == FAILURE)        {                php_error(E_WARNING, "zend_parse_parameters failure!");                                return;        }        RETURN_LONG(x + y);}PHP_FUNCTION(b){        char *str = NULL;        int argc = ZEND_NUM_ARGS();        int str_len;        long n;        char *result;        char *ptr;        int result_length;        if (zend_parse_parameters(argc TSRMLS_CC, "sl", &str, &str_len, &n) == FAILURE)        {                        php_error(E_WARNING, "zend_parse_parameters failure!");                                return;        }        result_length = str_len * n;        result = (char *) emalloc(result_length + 1);        ptr = result;        while (n--) {                memcpy(ptr, str, str_len);                ptr += str_len;        }        *ptr = '/0';        RETURN_STRINGL(result, result_length, 0);}

7、test_extension目錄下執行:/usr/local/bin/phpize

Configuring for:PHP Api Version:         20121113Zend Module Api No:      20121212Zend Extension Api No:   220121212

8、配置:./configure –with-php-config=/usr/local/bin/php-config
9、編譯:make
10、安裝:make install
安裝完成後/usr/local/lib/php/extensions/no-debug-zts-20121212/下會產生test_extension.so

11、修改php.in,加上:extension=test_extension.so

聯繫我們

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