Communication between PHP and application

I am playing with an embedded Linux device and am looking for a way to get the application code to interact with the web interface. I need to show some status information from an application on the web interface of the devices, and would also like to be able to inform the application of any user action like downloaded files, etc. PHP is a good way to make an interface, but the Communication side is more complicated. I found the following options, but I'm not sure what would be the easiest and most convenient to use.

Sockets. You need to enable sockets for PHP first to try this. I don't know if the resolution will take up much more space.

Database. Seems like an overflow solution.

Common file. Sounds like a lot of work.

Named pipes. Tried this with some success, but not sure if there will be problems like loading the page at the same time. Maybe sockets are simpler?

What's the best way? Is there something I am completely missing? How is this done in these many commercial Linux-based network switches?

+1


source to share


4 answers


I recently did something very similar using sockets and it worked pretty well. I had a Java application that talks to a device that was listening on a server socket and a PHP application was a client.

So, in your case, the PHP client initializes the connection and then the server can respond with the device status.



There are many tutorials on how to do client / server communication in most languages, so don't think too much.

+1


source


What kind of device is it?

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

How will pipe names cause concurrency problems that sockets will avoid?



From a device communication point of view in PHP, the file seems to be perfect. PHP can use something basic like file_get_contents (), the device can just write to a file. If you are concerned about a point in time, the file is updated to a quick length check.

In terms of PHP telling the device what to do, I also lean towards files. Ask the device to look at the directory and the script will create a file there with something like file_put_contents ($ path. Uniqid (), $ command); Thus, the two scripts run at the exact time, you just have two files for the device to work.

+1


source


The linux embedded boxes do not use PHP for routing using the web interface. They use CGI and have shell scripts that provide a web page.

For getting information from the application in the web interface, the Shared file parameter seems to me the most reasonable. The application can simply write information to a file that is read by PHP.

On the other hand, it doesn't look so good at first. PHP supports file locking, but most likely it doesn't work at the system level. Perhaps one solution is that in fact every PHP script that has information for the application creates its own file (with a unique filename id, for example based on timestamp + random value). The application can see a pop-up window with the specified directory for these files. Once processed, you can simply delete them. To do this, the app only needs write permission on the directory (so file ownership is not an issue).

0


source


Use shell scripts if possible.

I did something like this, I wrote a video surveillance application. Part of the video is processed by motion (large FOSS package). The application is a turnkey solution based on standardized equipment that is used to monitor casino slot machines. It serves as a local kiosk system and is accessible over the Internet. I wrote all the UI code in PHP, the local screen is a tightly closed KDE desktop with a full screen browser, by default on localhost. I used shell scripts to interact with motion and OS.

With another thought: If you can use self-compiled applications on a device: Write a simple program that returns the required value and use PHP exec () or passthru () or system ().

0


source







All Articles