从 php 加载 c 库

发布于 2024-08-27 10:41:37 字数 148 浏览 8 评论 0原文

在最近的一个项目中,我真的需要 lib tre 匹配库。

然而该项目是用 php 编写的,并且该库没有 php 绑定。

我试图用谷歌搜索如何为 c libs 创建接口,但我发现的只是 dl 函数,它只加载 php 扩展。

我缺少什么?

in a recent project I've come really need the lib tre matching library.

However the project is in php, and there are no php bindings for the library.

I've tried to google how to create an interface for c libs, but all I found was the dl function which seams to load only php extensions.

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

穿越时光隧道 2024-09-03 10:41:37

如果不存在“php 绑定”,则看起来您必须开发它们;-)

这是通过PHP 扩展完成的 - - 例如:

  • mysql< /code> 扩展,用于与 MySQL 通信,绑定 libmysql 库(PHP <= 5.2)
  • curl 扩展,这是一个围绕curl 库
  • 和许多其他库的包装器...

如果您想了解有关编写 PHP 扩展的更多信息,您可能会对这些链接感兴趣:

(请注意,这不是一件容易的任务 - 但如果您需要...那么 ^^ ;有些人会说这并不那么难)

而且,如果您真的 对这个主题感兴趣,并准备花一些钱,你可以购买这本书 扩展和嵌入 PHP (某些页面也可在 Google 图书上预览;它被认为是对这个主题感兴趣时值得一读的书(事实上,我很久以前就买了它,而且,在我看来,这确实是一本有趣的读物)

顺便说一句,那本书的作者也是我链接到的前四篇文章的作者;-)

If no "php bindings" exist, it looks like you'll have to develop them ;-)

This is done via a PHP extension -- such as, for example :

  • the mysql extension, that's used to communicate with MySQL, binding the libmysql library (with PHP <= 5.2)
  • The curl extension, that's a wrapper arround the curl library
  • and so many others...

If you want to learn more about writing PHP extensions, those links will probably interest you :

(Note that it's not quite an easy task -- but if you are required to... well ^^ ; and some would say it's not that hard )

And, if you are really interested by the subject, and ready to spend some money on it, you could buy the book Extending and Embedding PHP (some pages are available as preview on Google Books too) ; It's considered as the book to read when interested on this subject (In fact, I've bought it some time ago, and, in my opinion, it is indeed an interesting read)

BTW, the author of that book is also the author of the first four articles I linked to ;-)

故事灯 2024-09-03 10:41:37

编写一个向 PHP 公开 tre 的扩展(或者找到一个已经这样做的扩展)。 此处是一个很好的起点。

请注意,您将无法在大多数托管服务上加载您的扩展。

Write an extension that exposes tre to PHP (or find one that already does). A good starting point is here.

Be warned that you won't be able to load your extension on most hosting services.

情定在深秋 2024-09-03 10:41:37

自 PHP 7.4 起,捆绑了扩展 FFI(外部函数接口),但默认情况下禁用。

应该注意的是,截至撰写本文时,该扩展仍被视为实验性的,即其界面可能会更改,恕不另行通知。

示例

调用 libc 的 printf()

$ffi = FFI::cdef('int printf(const char *format, ...);', 'libc.so.6');

$ffi->printf('It %s!%s', 'works', PHP_EOL);

:非 CLI SAPI

默认情况下,FFI 仅适用于 PHP 的 CLI SAPI。对于 FPM 等其他 SAPI,有以下选项:

进一步阅读

Since PHP 7.4 the extension FFI (Foreign Function Interface) is bundled, but disabled by default.

It should be noted that as of this writing the extension is still considered EXPERIMENTAL, i.e. its interface may change without notice.

Example

Calling libc's printf():

$ffi = FFI::cdef('int printf(const char *format, ...);', 'libc.so.6');

$ffi->printf('It %s!%s', 'works', PHP_EOL);

Non-CLI SAPIs

By default FFI only works in PHP's CLI SAPI. For other SAPIs like FPM there are the following options:

Further reading

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文