cphp擴展
⑴ 如何編寫一個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 = '