php该如何调用用c语言或者go语言写的so库文件

这个so库文件没有用到php的任何东西,比如就是一个函数处理大小写而已,如何不要去编写php拓展直接调用呢?

阅读 3.2k
1 个回答

PHP 7.4版本(即将发行正式版,当前时间:2019年10月31日09:36:31)已经出了 FFI(Foreign Function Interface:外部函数接口),也就是说以后你可以不用写 C 扩展了,可以直接调用编译好的 so 库,不过该扩展当前状态为实验性的,稳定性有待验证。官方示例如下:

<?php
// create gettimeofday() binding
$ffi = FFI::cdef("
    typedef unsigned int time_t;
    typedef unsigned int suseconds_t;
 
    struct timeval {
        time_t      tv_sec;
        suseconds_t tv_usec;
    };
 
    struct timezone {
        int tz_minuteswest;
        int tz_dsttime;
    };
 
    int gettimeofday(struct timeval *tv, struct timezone *tz);    
", "libc.so.6");
// create C data structures
$tv = $ffi->new("struct timeval");
$tz = $ffi->new("struct timezone");
// call C's gettimeofday()
var_dump($ffi->gettimeofday(FFI::addr($tv), FFI::addr($tz)));
// access field of C data structure
var_dump($tv->tv_sec);
// print the whole C data structure
var_dump($tz);
?>

参考资料

  1. https://www.php.net/manual/en...
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题