PHP code example of techdock / opcua

1. Go to this page and download the library: Download techdock/opcua library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

techdock / opcua example snippets




use TechDock\OpcUa\Client\ClientBuilder;
use TechDock\OpcUa\Core\Types\NodeId;

// Connect to server
$client = ClientBuilder::create()
    ->endpoint('opc.tcp://localhost:4840')
    ->withAnonymousAuth()
    ->build();

// Read a value
$serverTime = $client->session->read([NodeId::numeric(0, 2258)])[0];
echo "Server time: {$serverTime->value}\n";

// Browse nodes
$objectsFolder = NodeId::numeric(0, 85);
$references = $client->browser->browse($objectsFolder);

foreach ($references as $ref) {
    echo "- {$ref->displayName->text}\n";
}

// Clean up
$client->disconnect();

$client = ClientBuilder::create()
    ->endpoint('opc.tcp://production-server:4840')
    ->application('My App', 'urn:mycompany:app')
    ->withUsernameAuth('operator', 'password')
    ->withCache(maxSize: 5000, ttl: 600.0)  // 10min cache
    ->withAutoBatching()                     // Auto-split large requests
    ->operationTimeout(60000)                // 60s timeout
    ->build();

// Create subscription
$subscription = $client->session->createSubscription(
    publishingInterval: 1000.0,  // 1 second
);

// Monitor a node
$nodeId = NodeId::numeric(2, 1001);
$subscription->createMonitoredItem(
    nodeId: $nodeId,
    samplingInterval: 500.0,
    callback: function ($value, $timestamp) {
        echo "Value changed: {$value} at {$timestamp}\n";
    }
);

// Process notifications
while (true) {
    $subscription->publishAsync();
    usleep(100000); // 100ms
}