Today is...
Saturday, June 15, 2019
The OPC Community Forum.
How to write PLC using PHP
How to write value to PLC using PHP script (web)

Hi All,

I want to write some value from my PHP script (web) to PLC, e.g.: when PHP give value 5, PLC will drive to fulfill tank 5 litres.

I know 1 of the way is using OPC, but I don't know how to write it on PHP.

Thank you..

There are different protocols for different PLC types.
Probably you will find a C/C++ library for communicating with your PLC.

If you want to use such a library from PHP you will need a language binding.
Such a language binding can be generated with

PS: PHP is not the language i would use.

What protocol does the PLC use?

>What protocol does the PLC use?

PLC using modbus TCP and modbus Serial. How to make it using one of them?

thank you

I have a Free Software (GPL) project on Sourceforge called MBLogic. It contains a number of programs. One of the programs is called HMIServer (it comes in the MBTools package). It has Modbus/TCP built into it. It comes in both server (slave) and client (master) versions.

HMIServer is intended to implement a web based HMI, but you could also use it as a proxy server for your PHP program. You would send an HTTP POST with a JSON payload to HMIServer and it would reply using HTTP and JSON. HMIServer will take care of reading and writing the Modbus/TCP data.

The JSON protocol is documented as the Cascadas protocol. There is a protocol document for this, and I can give you more details if you are interested.

In addition to this, there is another package called MBLogic (this is the name of a program as well as the name of the overall project) which has the same protocol. In addition to this, it also has a REST type web service protocol that lets you read and write arbitrary Modbus addresses using GET and POST. The MBLogic program has greater communications capabilities than HMIServer, so it is better suited if you have a large number of PLCs that you need to talk to.

In addition to these, I mentioned that HMIServer actually already implements a web based graphical HMI based on SVG. If what you want is simply to display data and allow operator input (e.g. push buttons), then it already does this off the shelf. It comes with push buttons, pilot lights, dials, gauges, tanks, etc. You can also draw your own art using Inkscape and control that. It uses an AJAX protocol to allow the page to be continuously updated without a page refresh.

All of this comes in both Linux and MS Windows versions. If any of the above interests you we can discuss this further either here, on the forums at the MBLogic project site (see the above links), or you can contact me directly (download a package and look in the source code files for a contact e-mail address).

This is all free/open source software, so it won't cost you anything.

> PLC using modbus TCP and modbus Serial. How to make it using one of them? <

It will be hard to find something in PHP.

But there are Modbus libraries in C or C++ like our implementation:

As said above you can generate a language binding for such a library with

Anyway, PHP only makes sense if you want to connect your PLC to a web page.
In this case you might run a separate program on your server (in C/C++) that writes the values into a MySQL database. This MySQL database can then be read from PHP.

Alternatively have a look on

PS: Remove the spaces in the URL above. The spaces are inserted by the forum software.


It used Kepware for the OPC. Is there anyone have a sample about PHP programming to give value at Kepware OPC?

thank you

Did you ever get this working?