php调用c接口无错版介绍

yipeiwu_com6年前PHP代码库

1.首先是要安装好PHP

2.进入PHP的下载解压目录下的ext目录
#cd /root/php-5.3.6/ext
#./ext_skel --extname=hmc

说明:
./ext_skel --extname=module_name
module_name是你自己可以选择的扩展模块的名字,例如我选择的hmc。执行工具后会自动在ext目录下建立你选择的module_name名字的目录,里面已经生成了相关的代码,这些代码中只需要调整config.m4文件中的三行注释就可以正常的编译带这个自定义扩展模块的php了。

3.修改config.m4
可以将生成的module目录复制到任意希望放置的地方
进入hmc目录
vi config.m4
使用文本编辑器打开config.m4文件
根据你自己的选择将
dnl PHP_ARG_WITH(hmc, for hmc support,
dnl Make sure that the comment is aligned:
dnl [ --with-hmc Include hmc support])
修改成
PHP_ARG_WITH(hmc for hmc support,
Make sure that the comment is aligned:
[ --with-hmc Include hmc support])
或者将
dnl PHP_ARG_ENABLE(hmc, whether to enable hmc support,
dnl Make sure that the comment is aligned:
dnl [ --enable-hmc Enable hmc support])
修改成
PHP_ARG_ENABLE(hmc, whether to enable hmc support,
Make sure that the comment is aligned:
[ --enable-hmc Enable hmc support])

一般我会选择后者,然后保存退出。

4.修改.c和.h文件代码
Vi hmc.c
将文件其中的下列代码进行修改
/* Every user visible function must have an entry in hmc_functions[].
*/
const zend_function_entry hmc_functions[] = {
PHP_FE(say_hello, NULL) /* 添加着一行代码 */
PHP_FE(confirm_hmc_compiled, NULL) /* For testing, remove later. */
{NULL, NULL, NULL} /* Must be the last line in hmc_functions[] */
};

在文件的最后添加下列代码
PHP_FUNCTION(say_hello)
{
zend_printf("hello world\n");
}
保存文件退出

vi php_hmc.h
在文件中PHP_FUNCTION(confirm_hmc_compiled);一行前面添加下面的代码
PHP_FUNCTION(say_hello);
保存文件退出

5.编译安装
#phpize
#./configure --enable-hmc
#make
#make install
应该在php的modules目录下有一个hmc.so文件,不同的机器位置会不一样



6.修改PHP配置
编辑php.ini,把扩展加入进去:
在[PHP]模块下增加:
extension = hmc.so
重启Httpd
#service httpd restart

7.php调用
建立一个c.php文件在对应的www目录下
<?
say_hello();
?>
这时浏览器应该输出:
hello world

8.调试
可以在命令行下输入
#php –q c.php
需要进入当前的www目录下

查看是否安装好模块可以用phpinfo()



也可以适用php -m命令
先用
#which php
找到php所在目录,不同机器目录不一样
#/usr/bin/php -m


9.带参数的函数
Vi hmc.c
修改最后的say_hello函数内容如下:
PHP_FUNCTION(say_hello)
{
zval **yourname;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &yourname) == FAILURE)
{
WRONG_PARAM_COUNT;
}
zend_printf("hello world, %s\n", Z_STRVAL_PP(yourname));
}

修改c.php为
<?
say_hello(“清清月儿");
?>

相关文章

PHP利用str_replace防注入的方法

PHP各种过滤字符函数复制代码 代码如下:   <?php    /**    * 安全过滤函数&n...

php上传文件的增强函数

表单中file对象,比如说$_FILES['doc'],$path为保存的路径,$type为限制上传的类型(他是一个数组,比如说限制上传doc和docx和格式,那这个参数可以填array...

令PHP初学者头疼十四条问题大总结

1、页面之间无法传递变量 get,post,session在最新的php版本中自动全局变量是关闭的,所以要从上一页面取得提交过来得变量要使用$_GET['foo'],$_POST['fo...

解决nginx不支持thinkphp中pathinfo的问题

解决nginx不支持thinkphp中pathinfo的问题

下面小编通过文字加代码的方式给大家详解下,具体内容如下: 其实,要解决nginx不支持pathinfo的问题,有两个解决思路,一是不使用pathinfo模式,二是修改nginx的配置文件...

php恢复数组的key为数字序列的方法

本文实例讲述了php恢复数组的key为数字序列的方法。分享给大家供大家参考。具体分析如下: 这里实现php把数组的key值恢复成类似于0,1,2,3,4,5...这样的数字序列 fu...