PHP 和应用程序之间的通信

发布于 2024-07-09 10:40:51 字数 487 浏览 4 评论 0原文

我正在使用嵌入式 Linux 设备,并寻找一种方法让我的应用程序代码与 Web 界面进行通信。 我需要在设备 Web 界面上显示应用程序的一些状态信息,并且还希望有一种方法可以通知应用程序任何用户操作,例如上传的文件等。PHP 似乎是制作界面的好方法,但是沟通部分比较困难。 我找到了以下选项,但不确定哪个是最简单且最方便使用的。

套接字。必须首先为 PHP 启用套接字才能尝试此操作。 不知道启用是否会占用更多空间。

数据库。似乎是一个矫枉过正的解决方案。

共享文件。看起来工作量很大。

命名管道。尝试过这一点并取得了一些成功,但不确定是否会出现问题,例如同时页面加载时。 也许套接字更容易?

最好的方法是什么? 有什么我完全想念的吗? 在众多基于 Linux 的商业网络交换机中,这是如何完成的?

I'm playing with an embedded Linux device and looking for a way to get my application code to communicate with a web interface. I need to show some status information from the application on the devices web interface and also would like to have a way to inform the application of any user actions like uploaded files etc. PHP-seems to be a good way to make the interface, but the communication part is harder. I have found the following options, but not sure which would be the easiest and most convenient to use.

Sockets. Have to enable sockets for the PHP first to try this. Don't know if enabling will take much more space.

Database. Seems like an overkill solution.

Shared file. Seems like a lot of work.

Named pipes. Tried this with some success, but not sure if there will be problems with for example on simultaneous page loads. Maybe sockets are easier?

What would be the best way to go? Is there something I'm totally missing? How is this done in those numerous commercial Linux based network switches?

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

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

发布评论

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

评论(4

百变从容 2024-07-16 10:40:51

我最近使用套接字做了一些非常类似的事情,而且效果非常好。 我有一个与设备通信的 Java 应用程序,它侦听服务器套接字,而 PHP 应用程序是客户端。

因此,在您的情况下,PHP 客户端将初始化连接,然后服务器可以回复设备的状态。

有很多关于如何使用大多数语言进行客户端/服务器套接字通信的教程,因此应该不会花太长时间来弄清楚。

I recently did something very similar using sockets, and it worked really well. I had a Java application that communicates with the device, which listened on a server socket, and the PHP application was the client.

So in your case, the PHP client would initialize the connection, and then the server can reply with the status of the device.

There's plenty of tutorials on how to do client/server socket communication with most languages, so it shouldn't take too long to figure out.

凝望流年 2024-07-16 10:40:51

它是什么样的设备?

如果您使用共享文件之类的东西,设备将如何更新?

命名管道将如何遇到套接字可以避免的并发问题?

就从设备到 PHP 的通信而言,文件似乎是完美的。 PHP 可以使用诸如 file_get_contents() 之类的基本功能,设备可以直接写入文件。 如果您担心文件更新的时间,可以进行快速长度检查。

就 PHP 通知设备要做什么而言,我也倾向于文件。 让设备监视一个目录,并让脚本在其中创建一个文件,其中包含类似 file_put_contents($path . uniqid(), $command); 的内容。 这样,如果两个脚本同时运行,您只需拥有两个文件供设备使用。

What kind of device is it?

If you work with something like a shared file, how will the device be updated?

How will named pipes run into concurrency problems that sockets will avoid?

In terms of communication from the device to PHP, a file seems perfect. PHP can use something basic like file_get_contents(), the device can just write to the file. If you're worried about the moment in time the file is updated to a quick length check.

In terms of PHP informing the device of what to do, I'm also leaning towards files. Have the device watch a directory, and have the script create a file there with something like file_put_contents($path . uniqid(), $command); That way should two scripts run at the exact sime time, you simply have two files for the device to work with.

梦旅人picnic 2024-07-16 10:40:51

用于通过 Web 界面进行路由的嵌入式 Linux 盒子不使用 PHP。 他们使用 CGI 并让 shell 脚本传送网页。

对于从应用程序到 Web 界面获取信息,共享文件选项对我来说似乎最合理。 应用程序只需将信息写入 PHP 读取的文件即可。

反过来说,一开始看起来不太好。 PHP 支持文件锁定,但它很可能在系统级别不起作用。 也许一种解决方案是,实际上每个具有应用程序信息的 PHP 脚本都会创建自己的文件(具有唯一的 ID 文件名,例如基于时间戳 + 随机值)。 应用程序可以监视指定的目录以弹出这些文件。 处理完后,就可以删除它们。 为此,应用程序只需要目录的写权限(因此文件所有权不是问题)。

Embedded linux boxes for routing with web interface don't use PHP. They use CGI and have shell scripts deliver the web page.

For getting information from the application to the web interface, the Shared file option seems most reasonable to me. The application can just write information into the file which is read by PHP.

The other way round it looks not so good at first. PHP supports locking of files, but it most probably doesn't work on a system level. Perhaps one solution is that in fact every PHP script which has information for the application creates it own file (with a unique id filename, e.g. based on timestamp + random value). The application could watch a designated directory for these files to pop-up. After processing them, it could just delete them. For that, the application only needs write permission on the directory (so file ownership is not an issue).

灼疼热情 2024-07-16 10:40:51

如果可能,请使用 shell 脚本。

我做了类似的事情,我写了一个视频监控应用程序。 视频部分由 Motion 处理(一个很棒的 FOSS 包)。 该应用程序是标准化硬件上的交钥匙解决方案,用于监控老虎机赌场。 它在本地充当信息亭系统,可通过互联网访问。 我用 PHP 编写了所有 UI 代码,本地显示是一个严格锁定的 KDE 桌面,全屏浏览器默认为 localhost。 我使用 shell 脚本与运动和操作系统进行交互。

再想一想:
如果您可以在设备上使用自编译的应用程序:编写一个返回您想要的值的简单程序,并使用 PHP 的 exec() 或 passthru() 或 system()。

If possible, use shell scripts.

I did something similar, i wrote a video surveillance application. The video part is handled by motion (a great FOSS package). The application is a turn-key solution on standardized hardware, used to monitor slot-machine casinos. It serves as a kiosk system locally and is accessible via internet. I wrote all UI code in PHP, the local display is a tightly locked down KDE desktop with a full screen browser defaulting to localhost. I used shell scripts to interact with motion and the OS.

On a second thought:
If you can use self-compiled applications on the device: Write a simple program that returns the value you want and use PHP's exec() or passthru() or system().

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