跨语言开发问题

发布于 2024-08-13 10:53:33 字数 345 浏览 4 评论 0原文

我正在开发一个涉及数据库 (My SQL)、网站 (PHP) 和自定义高性能服务器应用程序 (C++) 的项目。 C++ 应用程序(及其附带的客户端应用程序)构成了该项目的主要部分,数据库为其存储长期数据。该网站主要用于显示各种统计数据和管理。

1)我希望PHP脚本和C++应用程序能够以某种方式进行通信,因为数据库仅用于持久数据,另外C++应用程序可能会缓存一些东西,因此在某些情况下需要被告知重新加载数据。它们很可能位于不同的机器上,甚至可能位于不同的操作系统上。我一直在考虑这样的想法:TCP 可能是一些简单命令响应协议的最佳选择?

2) 编写一次通用数据库接口代码并能够在 PHP 网站和 C++ 应用程序中使用它的最佳方法是什么?

I'm working on a project that involves a database (My SQL), website (PHP) and a custom high performance server application (C++). The C++ application (and its accompanying client application) make up the main bulk of the project, with the database storing long term data for it. The website is primarily for displaying various statistics, and administration.

1) I want the PHP scripts and c++ application to be able to communicate in some way, since the database is only used for persistent data, and additionally the c++ application may cache some things so needs to be told to reload the data in some cases. It is highly likely they will be on different machines, and even possibly different OS's. I've been considering the idea that TCP may be the best option with some simple command - response protocol?

2) What is the best way to write the common database interface code once, and be able to use it from both the PHP website and the c++ applications?

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

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

发布评论

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

评论(8

孤单情人 2024-08-20 10:53:33

1)使用数据库进行通信。 C++ 应用程序可以

select * from table where some_last_modified_timestamp > '<last time checked>';

2) 在 PHP 和 C++ 中优先使用存储过程而不是硬编码查询。

1) Use the database to communicate. The C++ application can

select * from table where some_last_modified_timestamp > '<last time checked>';

2) Use stored procedures in preference to hardcoded queries both in PHP and C++.

二货你真萌 2024-08-20 10:53:33

您可能会尝试根本不允许 PHP 访问数据库。让 C++ 应用程序完成所有数据库工作,并使其向 PHP 站点提供数据。您可以将 C++ 应用程序的一部分作为服务器运行,以便 PHP 从中获取报告等。

You might try not allowing PHP to access the database at all. Make the C++ app do all the database work, and make it serve data to the PHP site. You could run part of the C++ app as a server for the PHP to fetch reports etc from it.

清泪尽 2024-08-20 10:53:33

#1:如果您使用不同的操作系统,那么 TCP 听起来是一个不错的主意。

#2:听起来您需要一个 C 库,然后从 C++(简单)和 PHP 调用它。在 Google 上搜索会返回许多有关用 C 语言编写 PHP 扩展的文章,例如:

http://devzone。 zend.com/article/1021

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

#1: If you're on different OSes, then TCP sounds like a decent idea.

#2: Sounds like you need a C library, and then call that from both C++ (trivial) and PHP. A search on Google returns lots of articles about writing PHP extensions in C, for example:

http://devzone.zend.com/article/1021

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

亢潮 2024-08-20 10:53:33

1)我还建议使用TCP。根据请求-响应的复杂性,我可能会选择一些临时文本协议或使用 XML(如果响应或请求是结构化且更复杂的,则特别适合)。如果您使用 XML,则无需编写自己的解析器/生成器。您甚至可以尝试使用 XML-RPC,但我还没有这方面的实践经验。

1) I would also suggest TCP. Depending on the complexity of request-response I'd probably pick either some ad-hoc text protocol or use XML (especially suitable if responses or requests are structured and more complex). If you use XML you won't need to write your own parsers/generators. You could even try using XML-RPC but I have no practical experience with that yet.

小清晰的声音 2024-08-20 10:53:33

在 PHP 和 C++ 中使用相同 SQL 的最佳方法是准备好的语句。

一种很好的通信方式是一个人托管另一个人连接到的服务器(自定义/soap/rest)。 PHP 可以轻松地托管和连接,并且由于该代码是用 C 编写的,因此用 C++ 也应该很容易。

我想说,像 Eric Seppanen 建议的那样编写 PHP 扩展远远超出了项目的范围和需求。

Best way to use the same SQL in both PHP and C++ is prepared statements.

A good way to communicate is for one to host a server (custom/soap/rest) which the other connects to. PHP can easily both host and connect, and since that code is written in C it should be easy in C++ too.

Writing a PHP extension like Eric Seppanen suggest is way beyond the scope and need of your project Id say.

¢好甜 2024-08-20 10:53:33

使用 ThriftProtobufs (可能是 Avro)来声明通信协议并通过 TCP 套接字使用它。它将解决您的跨语言问题,而无需滚动自定义协议,并且您最终会在两侧获得实际对象(C++ 的静态类型!)。我见过 Thrift 如此成功地使用。

Use Thrift or Protobufs (possibly Avro) to declare a communication protocol and use that over a tcp socket. It'll solve your cross language problems without having to roll a custom protocol, and you end up with actual objects on both sides (statically typed for c++!). I've seen Thrift used like this very successfully.

请恋爱 2024-08-20 10:53:33

我的方法是使用 SWIG。我将它与 python 一起使用,但它也支持 PHP。

然后就可以很容易地从您的脚本中使用它。

其他解决方案可能是一些 RPC(这将允许服务器和 PHP 应用程序位于不同的地方)。

My approach is to use SWIG. I use it with python, but it supports PHP also.

Then it's very easy to use it from your script.

Other solutions could be some RPC (wich would allow to have the server and the PHP application in different places).

我不会写诗 2024-08-20 10:53:33

我是这里的初学者,所以如果我的想法非常糟糕,请原谅。

但为什么我们不能通过 TCP 传输 XML。有一个 C++ TCP 服务器和 PHP TCP 客户端。我认为 PHP 有非常强大的套接字 API

I am a beginner here, so please excuse if my idea is terribly bad.

But why cant we just transfer XML over TCP. Have a C++ TCP server and PHP TCP Client. I think PHP has a pretty powerful socket APIs

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