从 PHP 调用 C/C++ 库函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2479402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:38:10  来源:igfitidea点击:

Calling C/C++ library function from PHP

phpc++

提问by Anon. BlackBerry Developer

We have a PHP web app running on a customer's machine. For an update, we have a bit of code in C that we'd like to include as a native opaque library along with the PHP web app.

我们有一个在客户机器上运行的 PHP Web 应用程序。对于更新,我们有一些 C 代码,我们希望将其作为本机不透明库与 PHP Web 应用程序一起包含在内。

How does one go about calling a C/C++ lib. function from PHP?

如何调用 C/C++ 库。PHP 的函数?

It cannot be assumed that the PHP app, called by the web server, has any sort of permission to call an exec(), eval(), or system() type of function to execute a C wrapper driver which in turn uses the C/C++ library, so it would need to be a direct C library use from within the PHP code.

不能假设由 Web 服务器调用的 PHP 应用程序具有调用 exec()、eval() 或 system() 类型的函数来执行 C 包装驱动程序的任何类型的权限,而后者又使用 C /C++ 库,因此它需要在 PHP 代码中直接使用 C 库。

回答by St. John Johnson

Take a look at some of the Zend tutorials on Extension writing, this one in particular "Wrapping C++ Classes in a PHP Extension"

看看一些关于扩展编写的 Zend 教程,特别是“Wrapping C++ Classes in a PHP Extension”

回答by Emiel Bruijntjes

The answer by St. John Johnson is correct. But you can now also us the php-cpp library. It offers a much easier bridge between PHP and C++. See http://www.php-cpp.comfor more information.

圣约翰约翰逊的回答是正确的。但是您现在也可以使用 php-cpp 库。它在 PHP 和 C++ 之间提供了一个更简单的桥梁。有关更多信息,请参阅http://www.php-cpp.com

回答by DUNBlend

You can compile your code and use system, shell_exec or passthru functions to handle the output. Most web hosts allow you to compile c++ code, just ssh to your server, upload the code and compile it.

您可以编译代码并使用 system、shell_exec 或 passthru 函数来处理输出。大多数网络主机允许您编译 C++ 代码,只需 ssh 到您的服务器,上传代码并编译它。

回答by Bernardo Ramos

Another option is to have the C code as a daemon, always running, and the php script connect to it throught unix domain sockets or some existing library to exchange data.

另一种选择是将 C 代码作为守护进程,始终运行,并且 php 脚本通过 unix 域套接字或某些现有库连接到它以交换数据。

More info here

更多信息在这里

回答by Ankit Marothi

You can use ZEND to plug your C code into PHP.

您可以使用 ZEND 将 C 代码插入 PHP。

http://php.net/manual/en/internals2.ze1.zendapi.php

http://php.net/manual/en/internals2.ze1.zendapi.php