The few lines of code below open a socket to the hub and uses that to send commands to the irNetBox (with IP address 192.168.1.40):
<?php
function sendMessage($sock, $msg)
{
if ( !socket_send($sock , $msg , strlen($msg), 0 ) )
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not send message: $msg - [$errorcode] $errormsg" . PHP_EOL);
}
echo "Message sent: $msg";
// Read return one byte at a time until message complete (haveEOM)
$ret = "";
do
{
socket_recv($sock, $buf, 1, MSG_WAITALL);
$errorcode = socket_last_error();
if ($errorcode != 0)
{
$errormsg = socket_strerror($errorcode);
die("Could not receive data - [$errorcode] $errormsg" . PHP_EOL);
}
$ret = $ret.$buf;
}
while ( !haveEOM($ret) );
echo "Response received: " . PHP_EOL . $ret . PHP_EOL;
}
// Checks for the end of a message:
// - Single line message - look for "\n"
// - Multi-line message - it has an opening bracket '{' so look for '}'
function haveEOM($msg)
{
if ( (strpos($msg, "{") !== FALSE ) && ( strpos($msg, "}") === FALSE ) )
{
return FALSE;
}
else if ( strpos($msg, "\n") === FALSE )
{
return FALSE;
}
return TRUE;
}
// RedRat irNetBox #1
$addr = 'localhost'; // localhost
$port = 40000; // RedRatHub port
// Create a socket for IP version 4, TCP, IP Protocol
if (!($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create TCP/IP socket: [$errorcode] $errormsg" . PHP_EOL);
}
echo "TCP/IP socket created" . PHP_EOL;
//socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>5, "usec"=>0));
// Connect socket to irNetBox
if(!socket_connect($sock , $addr , $port))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not connect to RedRatHub $addr:$port - [$errorcode] $errormsg" . PHP_EOL);
}
echo "Connection established to RedRatHub $addr:$port" . PHP_EOL;
// Send Signal Transmission to RedRatHub
sendMessage($sock, "ip=\"192.168.1.40\" dataset=\"Sky+\" signal=\"Channel+\" output=\"1:70\"\n");
sendMessage($sock, "hq=\"list redrats\"\n");
socket_close($sock);
?>