Who Says PHP Isn't Good Enough for OPC UA?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MyrinNew
    Senior Member
    • Feb 2024
    • 5168

    #1

    Who Says PHP Isn't Good Enough for OPC UA?

    "PHP? For OPC UA? Come on."


    If you've ever tried to connect a PHP application to a PLC, a SCADA system, or any industrial device, you've probably heard that line. Maybe you said it yourself, opening a browser and searching for a library that didn't exist.


    That time is over.





    The Historical Gap

    OPC UA is the standard protocol for industrial automation. Motors, sensors, historians, SCADA systems, PLCs — the entire Industry 4.0 world speaks OPC UA. It's the OPC Foundation standard: battle-tested, secure, feature-rich. It is, essentially, the TCP/IP of the manufacturing industry.


    And yet, for years, PHP was left out.


    These were your options:


    1. An HTTP-to-OPC-UA gateway

    Spin up a separate process (Python, Node.js, a C++ binary), expose a REST API, and have PHP act as the HTTP client. Result: a more complex infrastructure, added latency, one more point of failure, and a second language to maintain in parallel.


    2. Compiled C/C++ extensions

    Some PHP libraries wrap C implementations via FFI or native extensions. But compiling .so files for every PHP version, on every server, in every CI pipeline is an operational nightmare. And if your shared host doesn't support that extension? Dead stop.


    3. Shell out to other languages

    Calling a Python or Node.js process from PHP. Fragile. Slow. Hard to debug. And again: two stacks, two teams, two deployments.


    4. Commercial Windows-only libraries

    COM bridges, proprietary solutions, Windows-dependent. Let's not even go there.


    The practical result? Anyone running an ERP in Laravel, a dashboard in Symfony, or even just a PHP API that needed to read a temperature from a Siemens S7 — had to face this reality: PHP wasn't "industrial enough."



    Where php-opcua Comes From

    The project was born out of a real need. Gianfrancesco Aurecchia, its creator and maintainer, had a PHP application for monitoring a production line. He needed to connect it to Siemens PLCs via OPC UA. Every available option was ugly, expensive, or both.


    Instead of working around the problem, he built the solution.


    The result — today — is an ecosystem of 7 packages, 147,000 lines of PHP, 2,649 tests, 5,204 assertions, and zero runtime dependencies beyond ext-openssl. An open-source project that implements the full OPC UA binary protocol natively in PHP.



    The Ecosystem: Package by Package

    1. opcua-client — The Core

    This is the core package. It implements the entire OPC UA binary protocol stack in pure PHP:
    • TCP transport and binary encoding/decoding
    • Secure channel with asymmetric and symmetric encryption
    • Session management
    • All major OPC UA services: browse, read, write, method call, subscriptions, history



    composer require php-opcua/opcua-client









    use PhpOpcua\Client\ClientBuilder;

    $client = ClientBuilder::create()
    ->connect('opc.tcp://192.168.1.100:4840');

    $temp = $client->read('ns=2;s=Temperature');
    echo $temp->getValue(); // 23.5






    Three lines. No config files, no XML, no gateway.


    10 security policies, from None up to Aes256_Sha256_RsaPss and the new ECC policies (nistP256, nistP384, brainpoolP256r1, brainpoolP384r1). Anonymous, username/password, and X.509 authentication. Persistent trust store with TOFU support.



    2. opcua-session-manager — The Answer to PHP's Stateless Model

    This was the biggest objection. OPC UA requires a persistent session: the initial handshake costs 50–200ms and must be repeated on every connection. PHP's request/response model destroys all state at the end of each request.


    The answer is the Session Manager: a long-lived ReactPHP daemon that keeps OPC UA sessions alive in memory, communicating with PHP processes over a Unix socket. The handshake happens once. Every subsequent request reuses the existing session — reducing per-request overhead from ~150ms to ~5ms.






    composer require php-opcua/opcua-session-manager

    # Start the daemon
    php bin/opcua-session-manager











    use PhpOpcua\SessionManager\Client\ManagedClient;

    // Same interface as the direct client
    $client = new ManagedClient();
    $client->connect('opc.tcp://localhost:4840');

    $value = $client->read('i=2259');







    ManagedClient implements the same OpcUaClientInterface as the direct client. Change one line, keep all your code. In production, the daemon runs under systemd or supervisord.





    3. laravel-opcua — Laravel-Native





    composer require php-opcua/laravel-opcua







    Service Provider, Facade, Artisan commands, configuration via config/opcua.php. Named connections like database config. Subscriptions that become Laravel Events. Logging with Laravel's logger. Caching with Redis. Testing with MockClient.






    use PhpOpcua\LaravelOpcua\Facades\Opcua;

    // Read from the default connection
    $temperature = Opcua::read('ns=2;s=Temperature');

    // Switch to a different PLC on the fly
    $level = Opcua::connection('tank-plc')
    ->read('ns=2;s=FillLevel');







    The OpcuaManager automatically checks whether the Session Manager daemon is running: if the Unix socket exists, traffic routes through the daemon for session persistence; otherwise it builds a direct client. Zero code changes to switch between development and production.





    4. symfony-opcua — The Symfony Bundle





    composer require php-opcua/symfony-opcua







    Dependency injection, semantic YAML configuration, autowiring of OpcUaClientInterface, a console command for the daemon, automatic Monolog integration, automatic PSR-16 cache pool injection, and 47 PSR-14 events dispatched through Symfony's event system.






    # config/packages/php_opcua_symfony_opcua.yaml
    php_opcua_symfony_opcua:
    connections:
    default:
    endpoint: '%env(OPCUA_ENDPOINT)%'











    use PhpOpcua\SymfonyOpcua\OpcuaManager;

    class PlcController
    {
    public function status(OpcuaManager $opcua): Response
    {
    $client = $opcua->connect();
    $value = $client->read('i=2259');
    return $this->json(['status' => $value->getValue()]);
    }
    }










    5. opcua-client-nodeset — 51 Companion Specs, Zero Manual Codecs

    The OPC Foundation publishes dozens of "companion specifications": standardized data models for Robotics, Machinery, MachineTool, ISA-95, CNC, MTConnect, and many more. Decoding these structures normally requires writing custom codecs by hand.


    opcua-client-nodeset ships 807 pre-generated PHP files from 51 companion specs. Enumerations as PHP BackedEnum, structures as typed DTOs with full IDE autocomplete.






    composer require php-opcua/opcua-client-nodeset











    use PhpOpcua\Nodeset\Robotics\RoboticsRegistrar;
    use PhpOpcua\Nodeset\Robotics\Enums\OperationalModeEnu meration;

    $client = ClientBuilder::create()
    ->loadGeneratedTypes(new RoboticsRegistrar())
    ->connect('opc.tcp://192.168.1.100:4840');

    $mode = $client->read(RoboticsNodeIds::OperationalMode)->getValue();
    // OperationalModeEnumeration::MANUAL_REDUCED_SPEED — not a raw integer










    6. opcua-cli — Explore Without Writing Code





    composer require php-opcua/opcua-cli

    opcua-cli browse opc.tcp://192.168.1.10:4840 /Objects
    opcua-cli read opc.tcp://192.168.1.10:4840 "ns=2;i=1001"
    opcua-cli watch opc.tcp://192.168.1.10:4840 "ns=2;s=Temperature"
    opcua-cli endpoints opc.tcp://192.168.1.10:4840
    opcua-cli trust opc.tcp://server:4840
    opcua-cli generate:nodeset MySpec.NodeSet2.xml







    Address space browsing, node read/write, real-time monitoring, endpoint discovery, trust store management, and PHP class generation from custom NodeSet2.xml files. Full security support, JSON output, debug logging.





    7. uanetstandard-test-suite — CI Without Compromise

    A Docker environment with 10 pre-configured OPC UA servers based on UA-.NETStandard (the OPC Foundation's reference implementation), covering all security policies, all data types, events, and historical data. The opcua-client test suite runs against real servers, not mocks.






    ./vendor/bin/pest # everything
    ./vendor/bin/pest tests/Unit/ # unit only
    ./vendor/bin/pest tests/Integration/ --group=integration # integration only







    CI runs on PHP 8.2, 8.3, 8.4, and 8.5 via GitHub Actions.





    Ecosystem by the Numbers

    Lines of PHP 147,000
    Tests 2,649
    Assertions 5,204
    Packages 7
    Security policies 10 (RSA + ECC)
    Companion specs 51
    Pre-generated types 807
    C runtime dependencies 0
    Supported PHP versions 8.2 / 8.3 / 8.4 / 8.5





    The Architecture at a Glance





    Your Application

    ├── laravel-opcua or symfony-opcua Framework integration
    │ │
    │ ├── opcua-session-manager Persistent sessions (optional)
    │ │
    │ └── opcua-client Binary protocol, crypto, I/O
    │ │
    │ └── opcua-client-nodeset Type definitions (optional)

    └── opcua-cli Developer tooling







    Each package has a single responsibility. You can use just opcua-client in a CLI script, or the entire stack in an enterprise Laravel application. The only dependency required everywhere is ext-openssl.





    What You Can Build With It Today

    Real-time dashboards — Read process variables from PLCs in Laravel, display charts, fire alerts: all in the same stack as your existing web application. No sidecar, no extra microservice.


    SCADA-to-ERP integration — Read production counters via OPC UA and write them directly into your PHP-based ERP, MES, or inventory system. In an Eloquent model. Through a queue. With all the tools you already know.


    IoT data collection — Use the Session Manager to maintain persistent connections to dozens of devices simultaneously. Collect sensor data, store it in your database, process it with Laravel queues or Symfony Messenger.


    Automated OPC UA server testing — Use MockClient for unit tests and the Docker test suite for CI integration. Validate your OPC UA server implementation with a PHP test suite, without needing a separate desktop client.





    Bonus: It's Already AI-Ready

    There's one more thing worth highlighting — because in 2026 it makes a real difference.


    Using a new library with an AI assistant — Cursor, Copilot, Claude — only works well if the AI actually knows the library. If it doesn't, it generates plausible-looking but wrong code: unsupported patterns, non-existent methods, broken configurations. You end up spending more time correcting the AI than you would have spent reading the docs.


    php-opcua has addressed this problem explicitly.


    Every package in the ecosystem ships three files designed specifically for AI model consumption:


    llms.txt Structured summary: API, key patterns, core examples
    llms-full.txt Complete documentation in LLM-optimized format
    llms-skills.md Skill file for AI assistants with usage rules and patterns


    Available for every package directly from the website:





















    And a centralized index for the entire ecosystem:














    What changes in practice? Point your AI assistant at llms-full.txt for the package you need, or load llms-skills.md as context, and from that moment the AI generates correct code: the right NodeId strings, the right fluent builders, the right typed DTOs, the right retry and security patterns. Zero tokens wasted correcting hallucinations about an API the model never had in its training data.


    The library isn't just AI-compatible — it's AI-first by design.





    Conclusion

    The historical PHP gap for OPC UA still lives on in tutorials, forum threads, and industrial conference slides. But in practice, from 2026, it's no longer a real problem.


    php-opcua is a mature, tested, documented, and actively maintained ecosystem. Zero C extensions. Zero gateways. Zero architectural compromises. The full OPC UA binary protocol, in pure PHP, with native integration for Laravel and Symfony.


    Next time someone tells you "PHP isn't good enough for OPC UA", you have a concrete answer.





    Resources:



    More...
Working...