本文目錄一覽:
- 1、php怎樣和c語言混合編程
- 2、如何用C語言編寫PHP擴展的詳解
- 3、如何用c語言在windows平台上開發php extension
- 4、php中怎麼將c語言代碼編譯為php可用擴展
- 5、關於用C語言寫的mysql訪問程序,作成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-hant/n/304858.html