sexevil 发表于 2017-3-26 11:49:43

php如何调用c接口无错版

  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, forhmc support,

dnl Make sure that the comment is aligned:

dnl [ --with-hmc Includehmc support])

修改成

PHP_ARG_WITH(hmc forhmc support,

Make sure that the comment is aligned:

[ --with-hmc Includehmc support])

或者将

dnl PHP_ARG_ENABLE(hmc, whether to enablehmc support,

dnl Make sure that the comment is aligned:

dnl [ --enable-hmc Enablehmc support])

修改成

PHP_ARG_ENABLE(hmc, whether to enablehmc support,

Make sure that the comment is aligned:

[ --enable-hmc Enablehmc 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,把扩展加入进去:
在模块下增加:
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(“清清月儿");
?>


  ====================================
  可以参考以下:
  http://www.phpchina.com/index.php?action-viewthread-tid-26870
  http://blog.csdn.net/taft/article/details/596291
  http://blog.csdn.net/heiyeshuwu/article/details/3453854
  http://blog.csdn.net/xiaocon/article/details/388953
  一本书《Extending and Embedding PHP》:http://ishare.iask.sina.com.cn/f/14424739.html
页: [1]
查看完整版本: php如何调用c接口无错版