Skip to content

Instantly share code, notes, and snippets.

@scr34m
Created May 20, 2015 18:37
Show Gist options
  • Save scr34m/14b10cc3d336c7b4e6ed to your computer and use it in GitHub Desktop.
Save scr34m/14b10cc3d336c7b4e6ed to your computer and use it in GitHub Desktop.
APC UPS status read over network
<?php
function htons($i)
{
return pack('n', $i);
}
function nstoh($d)
{
$length = unpack('n', $d);
return $length[1];
}
$fp = fsockopen("127.0.0.1", 3551, $errno, $errstr);
if (!$fp) {
return;
}
stream_set_blocking($fp, 1);
stream_set_timeout($fp, 5);
fwrite($fp, htons(6), 2);
fwrite($fp, 'status', 6);
$status = '';
while (!feof($fp)) {
$length = nstoh(fread($fp, 2));
if ($length == 0) {
break;
}
$status .= fread($fp, $length);
}
fclose($fp);
echo $status;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment