导航:首页 > 编程语言 > c编写php

c编写php

发布时间:2023-09-16 16:51:24

1. 如何用c语言在windows平台上开发php extension

如何使用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,修改编译参数

[plain] view plainprint?
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 plainprint?
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 plainprint?
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 plainprint?
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 plainprint?
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 plainprint?
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 plainprint?
01.$a = int_ext("12345678900");
02.var_mp($a);
03.$a = int_ext("123456789");
04.var_mp($a);
$a = int_ext("12345678900");
var_mp($a);
$a = int_ext("123456789");
var_mp($a);
结果输出:

[plain] view plainprint?
01.string(11) "12345678900"
02.int(123456789)

2. 如何用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 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修改编译参数

[plain] view plainprint?
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 plainprint?
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 plainprint?
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 plainprint?
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 plainprint?
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 plainprint?
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 plainprint?
01.$a = int_ext("12345678900");
02.var_mp($a);
03.$a = int_ext("123456789");
04.var_mp($a);
$a = int_ext("12345678900");
var_mp($a);
$a = int_ext("123456789");
var_mp($a);
结输:

[plain] view plainprint?
01.string(11) "12345678900"
02.int(123456789)

阅读全文

与c编写php相关的资料

热点内容
算法战书籍 浏览:575
卸载网络服务器是什么意思 浏览:123
菜鸟app的收货地址在哪里 浏览:488
服务器配什么显卡 浏览:369
动态壁纸不动了是怎么回事安卓 浏览:412
申万宏源app哪里看总盈利 浏览:133
单片机测电感电容 浏览:165
android在子线程中更新ui 浏览:694
算法分析师面试有什么要求 浏览:994
容器算法大全图解 浏览:69
cad后置命令失效 浏览:692
杀手阻击存档文件夹是哪一个 浏览:212
禁书pdf 浏览:920
没用app语音智能提醒怎么设置 浏览:502
linuxwiki安装 浏览:680
隔墙算法 浏览:174
安卓手机为什么app不通知 浏览:550
申请云服务器购买费用 浏览:115
云服务器镜像下载到本地 浏览:4
电脑文件夹名有横杠 浏览:154