⑴ 如何編寫一個php的C擴展

一、首先下載PHP源碼包,假設源碼包目錄為:/software/php-5.2.13
一、首先下載PHP源碼包,假設源碼包目錄為:/software/php-5.2.13
#> cd /software/php-5.2.13/ext
二、假設我們要開發一個名為caleng_mole的擴展,該擴展包含兩個函數:a--處理兩個整型相加和b-處理字元串重復輸出;
1、首先編寫一個函數定義文件,該文件編寫函數原型後綴為def,假設為:caleng_mole.def
int a(int x, int y)
string b(string str, int n)
2、通過擴展骨架生成器,將在ext目錄下自動建立擴展目錄caleng_mole
#> ./ext_skel --extname=caleng_mole --proto=caleng_mole.def
3、修改配置文件: #> vim /software/php-5.2.13/ext/caleng_mole/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_mole/caleng_mole.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_mole
#> /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_mole.so文件
在php.ini配置文件中加入: extension=caleng_mole.so.
搞定收工

⑵ 可以在php擴展中直接寫c語言

Linux下用C開發PHP擴展
一、首先下載PHP源碼包,假設源碼包目錄為:/software/php-5.2.13
一、首先下載PHP源碼包,假設源碼包目錄為:/software/php-5.2.13
#> cd /software/php-5.2.13/ext
二、假設我們要開發一個名為caleng_mole的擴展,該擴展包含兩個函數:a--處理兩個整型相加和b-處理字元串重復輸出;
1、首先編寫一個函數定義文件,該文件編寫函數原型後綴為def,假設為:caleng_mole.def
int a(int x, int y)
string b(string str, int n)

2、通過擴展骨架生成器,將在ext目錄下自動建立擴展目錄caleng_mole
#> ./ext_skel --extname=caleng_mole --proto=caleng_mole.def
3、修改配置文件: #> vim /software/php-5.2.13/ext/caleng_mole/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_mole/caleng_mole.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_mole
#> /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_mole.so文件
在php.ini配置文件中加入: extension=caleng_mole.so.
搞定收工
轉載,僅供參考。

⑶ windows上用c寫php擴展怎麼解決

如何使用C語言開發PHP擴展。

函數功能:php裡面的整數是有符號數,其內部實現其實就是long,不是unsigned 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 plainprint?
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,修改編譯參數

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

1:預定義
在home目錄,也可以其他任意目錄,寫一個文件,例如caleng_mole.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_mole --proto=/home/hm/caleng_mole.def

3:修改config.m4
去掉dnl的注釋

PHP_ARG_ENABLE(caleng_mole, whether to enable caleng_mole support,
Make sure that the comment is aligned:
[ --enable-caleng_mole Enable caleng_mole support])

4:修改caleng_mole.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 = '';
RETURN_STRINGL(result, result_length, 0);
}
/* }}} */


5:生成擴展庫
#cd ./caleng_mole
#/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_mole.so文件

7:修改php.ini
php.ini如果找不到可以從phpinfo()打出的信息看到
#cd /usr/local/php/lib/
php.ini增加擴展信息
extension=caleng_mole.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_mole的擴展,該擴展包含兩個函數:a--處理兩個整型相加和b-處理字元串重復輸出;
1、首先編寫一個函數定義文件,該文件編寫函數原型後綴為def,假設為:caleng_mole.def
int a(int x, int y)
string b(string str, int n)
2、通過擴展骨架生成器,將在ext目錄下自動建立擴展目錄caleng_mole
#> ./ext_skel --extname=caleng_mole --proto=caleng_mole.def
3、修改配置文件: #> vim /software/php-5.2.13/ext/caleng_mole/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_mole/caleng_mole.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 = '';
RETURN_STRINGL(result, result_length, 0);
}
三、編譯安裝,假設php的安裝目錄為:/usr/localhost/webserver/php
#> cd /software/php-5.2.13/ext/caleng_mole
#> /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_mole.so文件
在php.ini配置文件中加入: extension=caleng_mole.so.

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

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

~

⑹ C擴展PHP 是什麼意思,能舉個簡單的例子嗎

比如在win下的lamp環境吧,你想要php支持某個擴展不是需要把php.ini文件的那個注釋取消么,那個擴展是個文件類型為.dll的linux下為.soC語言能編譯成這兩種文件類型.或者說所有的擴展都是C編寫的.

⑺ php為什麼要用c來擴展

1、首先編寫一個函數定義文件,該文件編寫函數原型後綴為def,假設為:caleng_mole.def
int a(int x, int y)
string b(string str, int n)
2、通過擴展骨架生成器,將在ext目錄下自動建立擴展目錄caleng_mole
#> ./ext_skel --extname=caleng_mole --proto=caleng_mole.def
3、修改配置文件: #> vim /software/php-5.2.13/ext/caleng_mole/config.m4,將如下行的注釋標簽"dnl"去掉,修改後如下所示:
PHP_ARG_ENABLE(myfunctions, whether to enable myfunctions support,
Make sure that the comment is aligned:
[ --enable-myfunctions Enable myfunctions support])

⑻ php怎麼調用c擴展

在PHP.INI載入編寫的擴展,然後直接當系統函數一樣使用擴展裡面的函數,調用是很簡單的。

⑼ 如何利用C自定義實現PHP擴展

注意兩點即可:
第一,調用其它文件的函數或類,需要先用include,require,include_once,require_once這些函數包含一下其它文件,這四個函數的區別,你可以查一下手冊。
第二,對於類外面的函數,在文件包含以後,可以直接調用;對於類內部的函數,在文件包含以後,如果是靜態函數,就用 類名::函數名 進行調用,如果不是靜態函數,就需要先將類實例化,然後用 實例化的對象->函數名 進行調用。