如何使用 C++ 代码与 PHP 交互?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1502244/
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 02:50:54  来源:igfitidea点击:

How can I use C++ code to interact with PHP?

phpc++performanceloadcompiled

提问by qodeninja

I was reading somewhere that sometimes PHP is simply not fast enough and that compiled code has to sometimes "do the heavy lifting"

我在某处读到有时 PHP 速度不够快,并且编译的代码有时必须“做繁重的工作”

What is the api in C++ to do this?

C++ 中的 api 是什么来做到这一点?

回答by Pascal MARTIN

You can add functions/classes to PHP, programmed in C (and you can wrap a C++ class from C, if I remember correctly from an article I read some time ago), which might allow you to do some things faster -- if programmed well : no need for interpretation of PHP code ; only execution of machine code, which is generally way faster.

您可以向 PHP 添加函数/类,用 C 编程(并且您可以从 C 包装一个 C++ 类,如果我从前段时间读过的一篇文章中没记错的话),这可能会让您更快地做一些事情——如果编程好吧:不需要解释 PHP 代码;只执行机器代码,这通常更快。

To do that, you'll have to develop a PHP extension.

为此,您必须开发一个PHP 扩展

There are not that many resources available on the Internet about that, but these one might help you to start :

Internet 上关于此的可用资源并不多,但这些资源可能会帮助您开始:

And, specifically about the C++ part, this one might help too :

而且,特别是关于 C++ 部分,这个也可能有帮助:

If you are really interested by the subject, and ready to spend some money on it, you could also buy the book Extending and Embedding PHP(some pages are available as preview on Google Bookstoo); I've seen a couple of times that it was thebook to read when interested on this subject (In fact, I've bought it some time ago, and it's an interesting read)

如果您真的对这个主题感兴趣,并准备花一些钱,您还可以购买扩展和嵌入 PHP一书某些页面也可在 Google 图书上预览;我曾多次看到这本书是对这个主题感兴趣时阅读(实际上,我前段时间已经买了它,这是一本有趣的书)

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

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

回答by brianreavis

You can actually executecompiled applications without any sort of API:

您实际上可以没有任何 API 的情况下执行已编译的应用程序:

$output = exec('/path/to/yourapp');

Beyond that, you could always write a PHP extension. There's a good guide on the subject here: http://devzone.zend.com/article/1021

除此之外,您始终可以编写 PHP 扩展。这里有一个关于这个主题的很好的指南:http: //devzone.zend.com/article/1021

回答by VolkerK

swig, the Simplified Wrapper and Interface Generatorcan help you wrapping (existing) c++ into a php module.

swig,简化的包装器和接口生成器可以帮助您将(现有的)c++ 包装到 php 模块中。

SWIG is a software development tool that connects programs written in C and C++ with a variety of high-level programming languages. SWIG is used with different types of languages including common scripting languages such as Perl, PHP, Python, Tcl and Ruby.
SWIG 是一种软件开发工具,它将用 C 和 C++ 编写的程序与各种高级编程语言连接起来。SWIG 用于不同类型的语言,包括常见的脚本语言,如 Perl、PHP、Python、Tcl 和 Ruby。

回答by Wayne

Well you have not defined what you are trying to do, but if you need to the C++ interface, then look at the ext directory in the source code to understand how to write a PHP extension that you can then load and use from your PHP scripts.

好吧,您还没有定义您要尝试做什么,但是如果您需要 C++ 接口,请查看源代码中的 ext 目录以了解如何编写 PHP 扩展,然后您可以从 PHP 脚本加载和使用该扩展.

A couple of links that may help:

几个可能有帮助的链接:

http://www.devarticles.com/c/a/Cplusplus/Developing-Custom-PHP-Extensions-Part-1/http://devzone.zend.com/article/1021

http://www.devarticles.com/c/a/Cplusplus/Developing-Custom-PHP-Extensions-Part-1/ http://devzone.zend.com/article/1021