1. Go to this page and download the library: Download php-opcua/symfony-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/ */
php-opcua / symfony-opcua example snippets
use PhpOpcua\SymfonyOpcua\OpcuaManager;
class PlcController
{
public function __construct(private readonly OpcuaManager $opcua) {}
public function index(): Response
{
$client = $this->opcua->connect();
$value = $client->read('i=2259');
echo $value->getValue(); // 0 = Running
$client->disconnect();
}
}
use PhpOpcua\Client\Types\BuiltinType;
$client = $this->opcua->connect();
$client->write('ns=2;i=1001', 42, BuiltinType::Int32);
// Or let the client auto-detect the type
$client->write('ns=2;i=1001', 42);
$client->disconnect();
use PhpOpcua\Client\Event\DataChangeReceived;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
#[AsEventListener]
class StoreSensorReading
{
public function __invoke(DataChangeReceived $e): void
{
// Store $e->dataValue->getValue() with $e->clientHandle
}
}
use PhpOpcua\Client\OpcUaClientInterface;
class PlcService
{
public function __construct(private readonly OpcUaClientInterface $client) {}
public function readServerState(): int
{
return $this->client->read('i=2259')->getValue();
}
}
use PhpOpcua\Client\Testing\MockClient;
use PhpOpcua\Client\Types\DataValue;
$mock = MockClient::create()
->onRead('i=2259', fn() => DataValue::ofInt32(0));
// Inject into OpcuaManager via DI or reflection
$value = $mock->read('i=2259');
echo $value->getValue(); // 0
echo $mock->callCount('read'); // 1