Corrections are needed on the script:
These lines:
$strHost = "127.0.0.1";
$oSocket = fsockopen ("localhost", 5038, &$errno, &$errstr, 20);
Are changed to:
$strHost = "192.168.X.Y"; // address of pbx as in manager.conf bindaddr=192.168.X.Y
$oSocket = fsockopen ($strHost, 5038, &$errno, &$errstr, 20);