php是一个脚本语言,如果需要系统调用,需要用C语言编写一个扩展来实现,另外C是静态编译的,执行效率比PHP代码高很多.同样的运算代码,使用C来开发,性能会比PHP要提升数百倍.编写好的php扩展要在编译php源码时加进去.
①.:预定义
在home目录,也可以其他任意目录,写一个文件,例如caleng_module.def
内容是你希望定义的函数名以及参数:
int a(int x,int y)
string b(string str,int n)
执行命令,生成对应扩展目录
#./ext_skel --extname=caleng_module --proto=/home/hm/caleng_module.def
去掉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])
代码如下:
/* {{{ 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);
#cd ./caleng_module
#/usr/local/php/bin/phpize
#./configure --with-php-config=/usr/local/php/bin/php-config
#make
#make install
如上图所示
改目录下有生成的caleng_module.so文件
php.ini如果找不到可以从phpinfo()打出的信息看到
#cd /usr/local/php/lib/
php.ini增加扩展信息
extension=caleng_module.so
/usr/local/php/bin/php -m
①.0:PHP调用
下面是原文
Linux下用C开发PHP扩展
第二段:假设我们要开发一个名为caleng_module的扩展,该扩展包含两个函数:a--处理两个整型相加和b-处理字符串重复输出;
①.、首先编写一个函数定义文件,该文件编写函数原型后缀为def,假设为:caleng_module.def
int a(int x, int y)
string b(string str, int n)
# ./ext_skel --extname=caleng_module --proto=caleng_module.def
PHP_ARG_ENABLE(myfunctions, whether to enable myfunctions support,
[ ?--enable-myfunctions ? ? ? ? ? Enable myfunctions support])
?int x, y, z;
?if (zend_parse_parameters(argc TSRMLS_CC, "ll", x, y) == FAILURE)
?z = x + y;
?RETURN_LONG(z);
?char *str = NULL;
第三段:编译安装,假设php的安装目录为:/usr/localhost/webserver/php
# /usr/localhost/webserver/php/bin/phpize
# ./configure --with-php-config=/usr/localhost/webserver/php/bin/php-config
# make
# make install
在php.ini配置文件中加入: extension=caleng_module.so.
何使用C语言发PHP扩展
①.建立扩展骨架
[plain] view plaincopyprint?
./ext_skel --extname=int_ext
[ --enable-int_ext Enable int_ext support]) 两行前面dnl 修改:
01.1. dnl Otherwise use enable:
① dnl Otherwise use enable:
vi php_int_ext.h
# PHP_FUNCTION(confirm_int_ext_compiled); 面新增行 PHP_FUNCTION(int_ext);[plain] view plaincopyprint?
vi int_ext.c
#PHP_FE(confirm_int_ext_compiled, NULL) 面添加 PHP_FE(int_ext, NULL)添加:
① zend_function_entry int_ext_functions[] = {
核代码:
01.PHP_FUNCTION(int_ext)
①.0. result = (char *) emalloc(result_length + 1);
①.1. memcpy(result,str,result_length);
PHP_FUNCTION(int_ext)
char * str = NULL;
int str_len;
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);
if(result_num max_long)
RETURN_LONG(result_num);
else
RESULT_STRINGL(result, result_length, 0);
/usr/local/php/bin/pphpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
[int_ext]
extension = int_ext.so
var_dump($a);
结输:
还是得使用C语言的编译器,,,,,按PHP的扩展规范去写、编译、链接
~
编译时配置好依赖关系吧,然后如果PHP环境比较多,别弄错环境就好了.
以上就是土嘎嘎小编为大家整理的c语言开发php扩展流程相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!