您的位置:首页 > 编程语言 > PHP开发

php调用C代码的方法详解

2009-02-01 11:14 866 查看
在php程序中需要用到C代码,应该是下面两种情况:

1 已有C代码,在php程序中想直接用
2 由于php的性能问题,需要用C来实现部分功能

针对第一种情况,最合适的方法是用system调用,把现有C代码写成一个独立的程序。参数通过命令行或者标准输入传入,结果从标准输出读出。其次,稍麻烦一点的方法是C代码写成一个daemon,php程序用socket来和它进行通讯。

重点讲讲第二种情况,虽然沿用system调用的方法也可以,但是想想你的目的是优化性能,那么频繁的起这么多进程,当然会让性能下降。而写daemon的方法固然可行,可是繁琐了很多。

我的简单测试,同样一个算法,用C来写比用php效率能提高500倍。而用php扩展的方式,也能提高90多倍(其中的性能损失在了参数传递上了吧,我猜)。

所以有些时候php扩展就是我们的最佳选择了。

这里我着重介绍一下用C写php扩展的方法,而且不需要重新编译php。

首先,找到一个php的源码,php4或者php5版本的都可以,与你目标平台的php版本没有关系。

在源码的ext目录下可以找到名为ext_skel的脚本
在这个目录下执行
#./ext_skel --extname=hello
这时生成了一个目录 hello,目录下有几个文件,你只需要关心这三个:config.m4 hello.c php_hello.h

把这个目录拷备到任何你希望的地方,cd进去,依次执行
#phpize
#./configure
#make
什么也没发生,对吧?
这是因为漏了一步,打开config.m4,找到下面
dnl If your extension references something external, use with:

...
dnl Otherwise use enable:

...
这是让你选择你的扩展使用with还是enable,我们用with吧。把with那一部分取消注释。
如果你和我一样使用vim编辑器,你就会很容易发现dnl三个字母原来是表示注释的呀(这是因为vim默认带了各种文件格式的语法着色包)

我们修改了config.m4后,继续

#phpize
#./configure
#make
这时,modules下面会生成hello.so和hello.la文件。一个是动态库,一个是静态库。

你的php扩展已经做好了,尽管它还没有实现你要的功能,我先说说怎么使用这个扩展吧!ext_skel为你生成了一个hello.php里面有调用示例,但是那个例子需要你把hello.so拷贝到php的扩展目录中去,我们只想实现自己的功能,不想打造山寨版php,改用我下面的方法来加载吧:

if(!extension_loaded("hello")) {
dl_local("hello.so");
}
function dl_local( $extensionFile ) {
//make sure that we are ABLE to load libraries
if( !(bool)ini_get( "enable_dl" ) || (bool)ini_get( "safe_mode" ) ) {
die( "dh_local(): Loading extensions is not permitted./n" );
}

//check to make sure the file exists
if( !file_exists(dirname(__FILE__) . "/". $extensionFile ) ) {
die( "dl_local(): File '$extensionFile' does not exist./n" );
}

//check the file permissions
if( !is_executable(dirname(__FILE__) . "/". $extensionFile ) ) {
die( "dl_local(): File '$extensionFile' is not executable./n" );
}

//we figure out the path
$currentDir = dirname(__FILE__) . "/";
$currentExtPath = ini_get( "extension_dir" );
$subDirs = preg_match_all( "////" , $currentExtPath , $matches );
unset( $matches );

//lets make sure we extracted a valid extension path
if( !(bool)$subDirs ) {
die( "dl_local(): Could not determine a valid extension path [extension_dir]./n" );
}

$extPathLastChar = strlen( $currentExtPath ) - 1;

if( $extPathLastChar == strrpos( $currentExtPath , "/" ) ) {
$subDirs--;
}

$backDirStr = "";
for( $i = 1; $i <= $subDirs; $i++ ) {
$backDirStr .= "..";
if( $i != $subDirs ) {
$backDirStr .= "/";
}
}

//construct the final path to load
$finalExtPath = $backDirStr . $currentDir . $extensionFile;

//now we execute dl() to actually load the module
if( !dl( $finalExtPath ) ) {
die();
}

//if the module was loaded correctly, we must bow grab the module name
$loadedExtensions = get_loaded_extensions();
$thisExtName = $loadedExtensions[ sizeof( $loadedExtensions ) - 1 ];

//lastly, we return the extension name
return $thisExtName;

}//end dl_local()

这样的好处是你的php扩展可以随你的php代码走,绿色扩展。

随后一个让人关心的问题是,如何添加函数、实现参数传递和返回值

添加函数步骤如下:
php_hello.h:
PHP_FUNCTION(confirm_hello_compiled);// 括号里面填写函数名

hello.c

zend_function_entry hello_functions[] = {
PHP_FE(confirm_hello_compiled, NULL) /* 这里添加一行 */

{NULL, NULL, NULL} /* Must be the last line in hello_functions[] */
};

PHP_FUNCTION(confirm_hello_compiled)
{// 这里写函数体
}
要实现的函数原型其实都一个样,用宏PHP_FUNCTION来包装了一下,另外呢,在hello_functions里面添加了一行信息,表示你这个模块中有这个函数了。

那么都是一样的函数原型,如何区分返回值与参数呢?
我给一个例子:

PHP_FUNCTION(hello_strdiff)
{
char *r1 = NULL, *r2 = NULL;
int n = 0, m = 0;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &r1, &n, &r2, &m) == FAILURE) {
return;
}
while(n && m && *r1 == *r2) {
r1++;
r2++;
n--;
m--;
}

if(n == 0) RETURN_LONG(m);
if(m == 0) RETURN_LONG(n);
int d[n+1][m+1];
int cost;
int i,j;
for(i = 0; i <= n; i++) d[i][0] = i;
for(j = 0; j <= m; j++) d[0][j] = j;
for(i = 1; i <= n; i++) {
for(j = 1; j <= m; j++) {
if(r1[i-1] == r2[j-1]) cost = 0;
else cost = 1;
int a = MIN(d[i-1][j]+1,d[i][j-1]+1);
a = MIN(a, d[i-1][j-1]+cost);
d[i][j] = a;
}
}
RETURN_LONG(d
[m]);
}

这是一个求两个字符串差异度的算法,输入参数两个字符串,返回整型。
参数的传递看这里
zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &r1, &n, &r2, &m)

把这个当成是scanf来理解好了。
类型说明见下表:

Boolean
b
zend_bool
Long
l
long
Double
d
double
String
s
char*, int
Resource
r
zval*
Array
a
zval*
Object
o
zval*
zval
z
zval*
如果想实现可选参数的话,例如一个字符串,一个浮点,再加一个可选的bool型,可以用"sd|b"来表示。
和scanf有一点不同的是,对于字符串,你要提供两个变量来存储,一个是char *,存字符串的地址,一个int,来存字符串的长度。这样有必要的时候,你可以安全的处理二进制数据。

那么返回值怎么办呢?
使用下面一组宏来表示:
RETURN_STRING

RETURN_LONG

RETURN_DOUBLE

RETURN_BOOL

RETURN_NULL

注意RETURN_STRING有两个参数
当你需要复制一份字符串时使用
RETURN_STRING("Hello World", 1);

否则使用
RETURN_STRING(str, 0);

这里涉及到了模块中内存的分配,当你申请的内存需要php程序中去释放的话,请参照如下表

TraditionalNon-PersistentPersistent
malloc(count)


calloc(count, num)
emalloc(count)


ecalloc(count, num)
pemalloc(count, 1)
*

pecalloc(count, num, 1)
strdup(str)


strndup(str, len)
estrdup(str)


estrndup(str, len)
pestrdup(str, 1)


pemalloc() & memcpy()
free(ptr)
efree(ptr)
pefree(ptr, 1)
realloc(ptr, newsize)
erealloc(ptr, newsize)
perealloc(ptr, newsize, 1)
malloc(count * num + extr)
**
safe_emalloc(count, num, extr)
safe_pemalloc(count, num, extr)
一般我们使用Non-Persistent中列出的这些好了。

基本上就是这样,可以开始写一个php的扩展了。
从我目前的应用来看,能操纵字符串就够用了,所以我就只能介绍这么多了,如果要详细一点的呢,例如php数组怎么处理,可以参考
http://devzone.zend.com/node/view/id/1022

更详细的呢,可以参考php手册中的《Zend API:深入 PHP 内核》一章
不过这些资料都是英文的。

转自:http://blog.csdn.net/oyd/article/details/3168417



另外可参考:http://www.phpchina.com/index.php?action-viewthread-tid-26870

http://blog.csdn.net/xiaocon/article/details/388953
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: