c語言開發php擴展流程(php 擴展開發)

本文目錄一覽:

php怎樣和c語言混合編程

php是一個腳本語言,如果需要系統調用,需要用C語言編寫一個擴展來實現,另外C是靜態編譯的,執行效率比PHP代碼高很多。同樣的運算代碼,使用C來開發,性能會比PHP要提升數百倍。編寫好的php擴展要在編譯php源碼時加進去。

如何用C語言編寫PHP擴展的詳解

1:預定義

在home目錄,也可以其他任意目錄,寫一個文件,例如caleng_module.def

內容是你希望定義的函數名以及參數:

int a(int x,int y)

string b(string str,int n)

2:到php源碼目錄的ext目錄

#cd /usr/local/php-5.4.0/ext/

執行命令,生成對應擴展目錄

#./ext_skel –extname=caleng_module –proto=/home/hm/caleng_module.def

3:修改config.m4

去掉dnl的注釋

PHP_ARG_ENABLE(caleng_module, whether to enable caleng_module support,

Make sure that the comment is aligned:

[  –enable-caleng_module           Enable caleng_module support])

4:修改caleng_module.c

代碼如下:

/* {{{ proto int a(int x, int y)

   */

PHP_FUNCTION(a)

{

int argc = ZEND_NUM_ARGS();

int x;

int y;

   int z;

if (zend_parse_parameters(argc TSRMLS_CC, “ll”, x, y) == FAILURE)

 return;

z=x+y;

 RETURN_LONG(z);

}

/* }}} */

/* {{{ proto string b(string str, int n)

   */

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)

       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);

}

/* }}} */

5:生成擴展庫

#cd ./caleng_module

#/usr/local/php/bin/phpize

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

#make

#make install

6:到php的對應extensions目錄

如上圖所示

#cd /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/

改目錄下有生成的caleng_module.so文件

7:修改php.ini

php.ini如果找不到可以從phpinfo()打出的信息看到

#cd /usr/local/php/lib/

php.ini增加擴展信息

extension=caleng_module.so

8:重啟Apache

# /usr/local/apache2/bin/apachectl restart

9:檢查載入

/usr/local/php/bin/php -m

10:PHP調用

代碼如下:

echo a(1,2);

輸出 3  就說明成功了!

下面是原文

Linux下用C開發PHP擴展

一、首先下載PHP源碼包,假設源碼包目錄為:/software/php-5.2.13

一、首先下載PHP源碼包,假設源碼包目錄為:/software/php-5.2.13

# cd /software/php-5.2.13/ext

二、假設我們要開發一個名為caleng_module的擴展,該擴展包含兩個函數:a–處理兩個整型相加和b-處理字元串重複輸出;

1、首先編寫一個函數定義文件,該文件編寫函數原型後綴為def,假設為:caleng_module.def

int a(int x, int y)

string b(string str, int n)

2、通過擴展骨架生成器,將在ext目錄下自動建立擴展目錄caleng_module

# ./ext_skel –extname=caleng_module –proto=caleng_module.def

3、修改配置文件: # vim /software/php-5.2.13/ext/caleng_module/config.m4,將如下行的注釋標籤”dnl”去掉,修改後如下所示:

PHP_ARG_ENABLE(myfunctions, whether to enable myfunctions support,

Make sure that the comment is aligned:

[  –enable-myfunctions           Enable myfunctions support])

4、完善函數a和b的功能: # vim /software/php-5.2.13/ext/caleng_module/caleng_module.c

PHP_FUNCTION(a)

{

   int x, y, z;

   int argc = ZEND_NUM_ARGS();

   if (zend_parse_parameters(argc TSRMLS_CC, “ll”, x, y) == FAILURE)

       return;

   z = x + y;

   RETURN_LONG(z);

}

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)

       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);

}

三、編譯安裝,假設php的安裝目錄為:/usr/localhost/webserver/php

# cd /software/php-5.2.13/ext/caleng_module

# /usr/localhost/webserver/php/bin/phpize

# ./configure –with-php-config=/usr/localhost/webserver/php/bin/php-config

# make

# make install

現在將在/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613目錄下生成caleng_module.so文件

在php.ini配置文件中加入: extension=caleng_module.so.

如何用c語言在windows平台上開發php extension

 何使用C語言發PHP擴展

函數功能:php面整數符號數其內部實現其實longunsigned long於32位機器說php能表示整數2^31-1般應用碰於2^31-1於2^32數能用字元串表示於mixed int_ext(string in)說字元串in表示整數於2^31-1返整數於返字元串

發擴展步驟:(首先需要載php源碼載php-5.3.14)

1建立擴展骨架

[plain] view plaincopyprint?

01.cd php-5.3.14/ext

02../ext_skel –extname=int_ext

cd php-5.3.14/ext

./ext_skel –extname=int_ext

2修改編譯參數

[plain] view plaincopyprint?

01.cd php-5.3.14/ext/int_ext

02.vi config.m4

cd php-5.3.14/ext/int_ext

vi config.m4掉 PHP_ARG_ENABLE(int_ext, whether to enable int_ext support

[ –enable-int_ext Enable int_ext support]) 兩行前面dnl 修改:

[plain] view plaincopyprint?

01.1. dnl Otherwise use enable:

02.2. PHP_ARG_ENABLE(int_ext, whether to enable int_ext support,

03.3. dnl Make sure that the comment is aligned:

04.4. [ –enable-int_ext Enable int_ext support])

1. dnl Otherwise use enable:

2. PHP_ARG_ENABLE(int_ext, whether to enable int_ext support,

3. dnl Make sure that the comment is aligned:

4. [ –enable-int_ext Enable int_ext support])

3編寫C代碼

[plain] view plaincopyprint?

01.cd php-5.3.14/ext/int_ext

02.vi php_int_ext.h

03.# PHP_FUNCTION(confirm_int_ext_compiled); 面新增行 PHP_FUNCTION(int_ext);

cd php-5.3.14/ext/int_ext

vi php_int_ext.h

# PHP_FUNCTION(confirm_int_ext_compiled); 面新增行 PHP_FUNCTION(int_ext);[plain] view plaincopyprint?

01.cd php-5.3.14/ext/int_ext

02.vi int_ext.c

03.#PHP_FE(confirm_int_ext_compiled, NULL) 面添加 PHP_FE(int_ext, NULL)添加:

04.1. zend_function_entry int_ext_functions[] = {

05.2. PHP_FE(confirm_int_ext_compiled, NULL) /* For testing, remove later. */

06.3. PHP_FE(int_ext, NULL) /* For testing, remove later. */

07.4. {NULL, NULL, NULL} /* Must be the last line in int_ext_functions[] */

08.5. };

cd php-5.3.14/ext/int_ext

vi int_ext.c

#PHP_FE(confirm_int_ext_compiled, NULL) 面添加 PHP_FE(int_ext, NULL)添加:

1. zend_function_entry int_ext_functions[] = {

2. PHP_FE(confirm_int_ext_compiled, NULL) /* For testing, remove later. */

3. PHP_FE(int_ext, NULL) /* For testing, remove later. */

4. {NULL, NULL, NULL} /* Must be the last line in int_ext_functions[] */

5. };

核代碼:

[plain] view plaincopyprint?

01.PHP_FUNCTION(int_ext)

02.{

03. char * str = NULL;

04. int str_len;

05. int argc = ZEND_NUM_ARGS();

06. if(zend_parse_parameters(argc TSRMLS_CC,”s”,str,str_len) == FAILURE)

07. return ;

08. char * result;

09. int result_length = str_len;

10. result = (char *) emalloc(result_length + 1);

11. memcpy(result,str,result_length);

12. unsigned long result_num = strtoul(result, NULL, 10);

13. int sizeoflong sizeof(long);

14. unsigned long max_long = 1 (sizeoflong * 8 -1);

15. if(result_num max_long)

16. {

17. RETURN_LONG(result_num);

18. }

19. else

20. {

21. RESULT_STRINGL(result, result_length, 0);

22. }

23.}

PHP_FUNCTION(int_ext)

{

char * str = NULL;

int str_len;

int argc = ZEND_NUM_ARGS();

if(zend_parse_parameters(argc TSRMLS_CC,”s”,str,str_len) == FAILURE)

return ;

char * result;

int result_length = str_len;

result = (char *) emalloc(result_length + 1);

memcpy(result,str,result_length);

unsigned long result_num = strtoul(result, NULL, 10);

int sizeoflong sizeof(long);

unsigned long max_long = 1 (sizeoflong * 8 -1);

if(result_num max_long)

{

RETURN_LONG(result_num);

}

else

{

RESULT_STRINGL(result, result_length, 0);

}

}

4編譯

[plain] view plaincopyprint?

01.cd php-5.3.14/ext/int_ext

02./usr/local/php/bin/pphpize

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

04.make

05.make install

cd php-5.3.14/ext/int_ext

/usr/local/php/bin/pphpize

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

make

make install

產so文件: /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/int_ext.so

修改php.ini 添加擴展extension_dir = “/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/”

[int_ext]

extension = int_ext.so

5測試

[plain] view plaincopyprint?

01.$a = int_ext(“12345678900”);

02.var_dump($a);

03.$a = int_ext(“123456789”);

04.var_dump($a);

$a = int_ext(“12345678900”);

var_dump($a);

$a = int_ext(“123456789”);

var_dump($a);

結輸:

[plain] view plaincopyprint?

01.string(11) “12345678900”

02.int(123456789)

php中怎麼將c語言代碼編譯為php可用擴展

還是得使用C語言的編譯器,,,,,按PHP的擴展規範去寫、編譯、鏈接

~

關於用C語言寫的mysql訪問程序,作成PHP擴展的問題。

編譯時配置好依賴關係吧,然後如果PHP環境比較多,別弄錯環境就好了。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/304858.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2025-01-01 11:06
下一篇 2025-01-01 11:06

相關推薦

  • PHP和Python哪個好找工作?

    PHP和Python都是非常流行的編程語言,它們被廣泛應用於不同領域的開發中。但是,在考慮擇業方向的時候,很多人都會有一個問題:PHP和Python哪個好找工作?這篇文章將從多個方…

    編程 2025-04-29
  • AES加密解密演算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密演算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES演算法,並對實現過程進…

    編程 2025-04-29
  • 學習Python對學習C語言有幫助嗎?

    Python和C語言是兩種非常受歡迎的編程語言,在程序開發中都扮演著非常重要的角色。那麼,學習Python對學習C語言有幫助嗎?答案是肯定的。在本文中,我們將從多個角度探討Pyth…

    編程 2025-04-29
  • Python被稱為膠水語言

    Python作為一種跨平台的解釋性高級語言,最大的特點是被稱為”膠水語言”。 一、簡單易學 Python的語法簡單易學,更加人性化,這使得它成為了初學者的入…

    編程 2025-04-29
  • OpenJudge答案1.6的C語言實現

    本文將從多個方面詳細闡述OpenJudge答案1.6在C語言中的實現方法,幫助初學者更好地學習和理解。 一、需求概述 OpenJudge答案1.6的要求是,輸入兩個整數a和b,輸出…

    編程 2025-04-29
  • PHP怎麼接幣

    想要在自己的網站或應用中接受比特幣等加密貨幣的支付,就需要對該加密貨幣擁有一定的了解,並使用對應的API進行開發。本文將從多個方面詳細闡述如何使用PHP接受加密貨幣的支付。 一、環…

    編程 2025-04-29
  • Python按位運算符和C語言

    本文將從多個方面詳細闡述Python按位運算符和C語言的相關內容,並給出相應的代碼示例。 一、概述 Python是一種動態的、面向對象的編程語言,其按位運算符是用於按位操作的運算符…

    編程 2025-04-29
  • Python語言由荷蘭人為中心的全能編程開發工程師

    Python語言是一種高級語言,很多編程開發工程師都喜歡使用Python語言進行開發。Python語言的創始人是荷蘭人Guido van Rossum,他在1989年聖誕節期間開始…

    編程 2025-04-28
  • Python語言設計基礎第2版PDF

    Python語言設計基礎第2版PDF是一本介紹Python編程語言的經典教材。本篇文章將從多個方面對該教材進行詳細的闡述和介紹。 一、基礎知識 本教材中介紹了Python編程語言的…

    編程 2025-04-28
  • Python語言實現人名最多數統計

    本文將從幾個方面詳細介紹Python語言實現人名最多數統計的方法和應用。 一、Python實現人名最多數統計的基礎 1、首先,我們需要了解Python語言的一些基礎知識,如列表、字…

    編程 2025-04-28

發表回復

登錄後才能評論