您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

php调用C代码的实现方法

2024/4/28 0:23:02发布5次查看
本篇文章主要是对php调用c代码的实现方法进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助
在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的脚本(windows平台使用ext_skel_win32.php)
在这个目录下执行./ext_skel --extname=hello(我用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 libraries06.        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 exists11.        if( !file_exists(dirname(__file__) . /. $extensionfile ) ) {
                die( dl_local(): file '$extensionfile' does not exist./n );
        }
        //check the file permissions16.        if( !is_executable(dirname(__file__) . /. $extensionfile ) ) {
                die( dl_local(): file '$extensionfile' is not executable./n );
        }
        //we figure out the path21.        $currentdir = dirname(__file__) . /;
        $currentextpath = ini_get( extension_dir );
        $subdirs = preg_match_all( //// , $currentextpath , $matches );
        unset( $matches );
        //lets make sure we extracted a valid extension path27.        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                 $backdirstr .= ..;
                if( $i != $subdirs ) {
                  $backdirstr .= /;
                }
        }
        //construct the final path to load46.        $finalextpath = $backdirstr . $currentdir . $extensionfile;
        //now we execute dl() to actually load the module49.        if( !dl( $finalextpath ) ) {
                die();
        }
        //if the module was loaded correctly, we must bow grab the module name54.        $loadedextensions = get_loaded_extensions();
        $thisextname = $loadedextensions[ sizeof( $loadedextensions ) - 1 ];
        //lastly, we return the extension name58.        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里面添加了一行信息,表示你这个模块中有这个函数了。
那么都是一样的函数原型,如何区分返回值与参数呢?
我给一个例子:
复制代码 代码如下:
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录