PHP code example of techdock / opcua-webapi-client

1. Go to this page and download the library: Download techdock/opcua-webapi-client 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-webapi-client example snippets



TechDock\OpcUaWebApiClient\Api\DefaultApi;
use TechDock\OpcUaWebApiClient\Configuration;
use TechDock\OpcUaWebApiClient\Model\ReadRequest;
use TechDock\OpcUaWebApiClient\Model\ReadValueId;
use TechDock\OpcUaWebApiClient\Model\RequestHeader;
use GuzzleHttp\Client;

// Configure the API endpoint
$config = Configuration::getDefaultConfiguration();
$config->setHost('https://webapi.opcfoundation.org/opcua');

// Create HTTP client
$httpClient = new Client([
    'timeout' => 30.0,
    'connect_timeout' => 10.0,
]);

// Initialize API
$api = new DefaultApi($httpClient, $config);

// Create a read request
$readRequest = new ReadRequest([
    'max_age' => 0,
    'nodes_to_read' => [
        new ReadValueId([
            'node_id' => 'i=2258',  // Server.ServerStatus.CurrentTime
            'attribute_id' => 13     // Value attribute
        ])
    ],
    'request_header' => new RequestHeader([
        'request_handle' => 1,
        'timestamp' => new \DateTime(),
        'timeout_hint' => 60000
    ])
]);

// Execute the read operation
$response = $api->read($readRequest);

// Access the result
$result = $response->getResults()[0];
echo "Server time: " . $result->getValue() . "\n";

use TechDock\OpcUaWebApiClient\Model\ReadRequest;
use TechDock\OpcUaWebApiClient\Model\ReadValueId;
use TechDock\OpcUaWebApiClient\Model\RequestHeader;

$readRequest = new ReadRequest([
    'max_age' => 0,
    'nodes_to_read' => [
        new ReadValueId([
            'node_id' => 'i=2258',     // Server.ServerStatus.CurrentTime
            'attribute_id' => 13        // Value attribute
        ])
    ],
    'request_header' => new RequestHeader([
        'request_handle' => 1,
        'timestamp' => new \DateTime(),
        'timeout_hint' => 60000
    ])
]);

$response = $api->read($readRequest);
$value = $response->getResults()[0]->getValue();

$readRequest = new ReadRequest([
    'max_age' => 0,
    'nodes_to_read' => [
        new ReadValueId([
            'node_id' => 'i=2258',     // Server.ServerStatus.CurrentTime
            'attribute_id' => 13
        ]),
        new ReadValueId([
            'node_id' => 'i=2259',     // Server.ServerStatus.State
            'attribute_id' => 13
        ])
    ],
    'request_header' => new RequestHeader([
        'request_handle' => 1,
        'timestamp' => new \DateTime(),
        'timeout_hint' => 60000
    ])
]);

$response = $api->read($readRequest);

foreach ($response->getResults() as $index => $result) {
    echo "Node {$index}: " . $result->getValue() . "\n";
}

use TechDock\OpcUaWebApiClient\Model\BrowseRequest;
use TechDock\OpcUaWebApiClient\Model\BrowseDescription;

$browseRequest = new BrowseRequest([
    'requested_max_references_per_node' => 20,
    'nodes_to_browse' => [
        new BrowseDescription([
            'node_id' => 'i=84',           // Root folder
            'browse_direction' => 0,        // Forward
            'reference_type_id' => 'i=33', // Hierarchical references
            'etText();
    echo "Found node: {$displayName}\n";
}

$browseDescription = new BrowseDescription([
    'node_id' => 'nsu=urn:opcfoundation.org:2024-10:starterkit:measurements;s=Measurements',
    'browse_direction' => 0,
    'reference_type_id' => 'i=33',
    '

try {
    $response = $api->read($readRequest);

    // Check for service-level errors
    $serviceResult = $response->getResponseHeader()->getServiceResult();
    if ($serviceResult && $serviceResult->getCode() !== null && $serviceResult->getCode() !== 0) {
        throw new \Exception('Operation failed with error code: ' . $serviceResult->getCode());
    }

    // Check individual result status codes
    foreach ($response->getResults() as $result) {
        if ($result->getStatusCode() && $result->getStatusCode()->getCode() !== 0) {
            echo "Warning: Result has non-Good status code\n";
        }
    }
} catch (\TechDock\OpcUaWebApiClient\ApiException $e) {
    echo "API Exception: " . $e->getMessage() . "\n";
    echo "HTTP Status Code: " . $e->getCode() . "\n";
}

use GuzzleHttp\Client;

$httpClient = new Client([
    'timeout' => 60.0,           // Overall timeout
    'connect_timeout' => 15.0,   // Connection timeout
    'debug' => false             // Enable for HTTP debug output
]);

$api = new DefaultApi($httpClient, $config);

$config = Configuration::getDefaultConfiguration();
$config->setHost('https://your-opcua-server.example.com/opcua');