Below is an example showing how you can subscribe to OPC item/value changes in PHP. QuickOPC-COM 5.01.404.1 or later is required.
IEasyDAClient.SubscribeItem.Main.zip (649 Bytes)
// $Header: $
// Copyright (c) CODE Consulting and Development, s.r.o., Plzen. All rights reserved.
//+++
// This example shows how subscribe to changes of a single item and display the value of the item with each change.
class DEasyDAClientEvents {
function ItemChanged($varSender, $varE)
{
print $varE->Vtq->ToString();
print "\n";
}
}
$EasyDAClient = new COM("OPCLabs.EasyDAClient.5.0");
$Events = new DEasyDAClientEvents();
com_event_sink($EasyDAClient, $Events, "DEasyDAClientEvents");
$EasyDAClient->SubscribeItem("", "OPCLabs.KitServer.2", "Simulation.Random", 1000);
print "Processing item changed events for 1 minute...\n";
for ($time = 0; $time < 60; $time++) com_message_pump(1000);
?>
Some related documentation: http://php.net/manual/en/function.com... .
Pay attention to the comment that says "Be careful how you use this feature; if you are doing something similar to the example below, then it doesn't really make sense to run it in a web server context.". What they are trying to say is that processing a web request should be a short-lived code, which does not fit well with the idea of being subscribed to events and received them over longer time. It is possible to write such code, but it is only useful when processing the request is allowed to take relatively long. Or, when you are using PHP from command-line, or otherwise - not to serve a web page directly.
Subscribing to QuickOPC-COM events in the context of PHP Web application, while not imposing the limitations to the request processing time, has to be "worked around", and will possibly be covered in a separate post.