Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
utils:fifo-client-php [2008/04/01 22:24] – PcUTIxhXRysNek 202.82.144.3 | utils:fifo-client-php [2008/04/03 16:32] (current) – old revision restored 212.227.35.68 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== FIFO Client PHP Example ====== | ||
+ | A sample implementation of a FIFO client in PHP you can integrate it in your web applications. | ||
+ | |||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | |||
+ | <table border=" | ||
+ | < | ||
+ | <form action="<? | ||
+ | <tr> | ||
+ | < | ||
+ | < | ||
+ | </tr> | ||
+ | </ | ||
+ | <tr align=" | ||
+ | <td colspan=" | ||
+ | < | ||
+ | </td> | ||
+ | </tr> | ||
+ | </ | ||
+ | |||
+ | <?php | ||
+ | $cmd = NULL; | ||
+ | if (isset($_POST[' | ||
+ | $cmd = $_POST[' | ||
+ | } | ||
+ | if (isset($_POST[' | ||
+ | $host=" | ||
+ | $port = 12345; | ||
+ | $pos = strpos($cmd," | ||
+ | if ($pos == NULL) { | ||
+ | $fifo_cmd = ": | ||
+ | } else { | ||
+ | $t1 = substr($cmd, | ||
+ | $t2 = substr($cmd, | ||
+ | $fifo_cmd = ": | ||
+ | } | ||
+ | $timeout = 5; | ||
+ | $fp = fsockopen ($host, $port, $errno, $errstr, $timeout); | ||
+ | if (!$fp) { | ||
+ | die(" | ||
+ | } else { | ||
+ | echo "< | ||
+ | // write the user string to the socket | ||
+ | fputs ($fp, $fifo_cmd); | ||
+ | // get the result | ||
+ | while ($result = fgets ($fp, 1024)) { | ||
+ | echo " | ||
+ | } | ||
+ | // close the connection | ||
+ | fclose ($fp); | ||
+ | } | ||
+ | } | ||
+ | ?> | ||
+ | |||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | Here is another example using the function write2fifo taken from serweb. | ||
+ | It should work as it with a default openser installation. | ||
+ | This script comes with no warranty... | ||
+ | Type **which** to list all available commands. | ||
+ | |||
+ | < | ||
+ | <?php | ||
+ | /* This script has part of it taken from " | ||
+ | | ||
+ | it must have the same value as SER's fifo config param | ||
+ | */ | ||
+ | $config-> | ||
+ | |||
+ | /* values used for names of reply fifos -- they change randomly | ||
+ | this values shouldn' | ||
+ | you doing | ||
+ | */ | ||
+ | $config-> | ||
+ | $config-> | ||
+ | |||
+ | /* This function is taken from " | ||
+ | function write2fifo($fifo_cmd, | ||
+ | global $config; | ||
+ | |||
+ | /* check if fifo is running */ | ||
+ | if (!file_exists($config-> | ||
+ | | ||
+ | |||
+ | / | ||
+ | NULL, "fifo path:" | ||
+ | echo "FIFO not running or bad path to it"; | ||
+ | return false; | ||
+ | } | ||
+ | |||
+ | |||
+ | /* open fifo now */ | ||
+ | $fifo_handle=fopen( $config-> | ||
+ | if (!$fifo_handle) { | ||
+ | $errors[]=" | ||
+ | } | ||
+ | |||
+ | /* create fifo for replies */ | ||
+ | @system(" | ||
+ | |||
+ | |||
+ | /* add command separator */ | ||
+ | $fifo_cmd=$fifo_cmd." | ||
+ | |||
+ | /* write fifo command */ | ||
+ | if (fwrite( $fifo_handle, | ||
+ | @unlink($config-> | ||
+ | @fclose($fifo_handle); | ||
+ | $errors[]=" | ||
+ | } | ||
+ | @fclose($fifo_handle); | ||
+ | |||
+ | /* read output now */ | ||
+ | @$fp = fopen( $config-> | ||
+ | if (!$fp) { | ||
+ | @unlink($config-> | ||
+ | $errors[]=" | ||
+ | } | ||
+ | |||
+ | $status=fgets($fp, | ||
+ | if (!$status) { | ||
+ | @unlink($config-> | ||
+ | $errors[]=" | ||
+ | } | ||
+ | |||
+ | |||
+ | $rd=""; | ||
+ | while (!feof($fp)) { | ||
+ | $rd.=fread($fp, | ||
+ | } | ||
+ | @unlink($config-> | ||
+ | |||
+ | return $rd; | ||
+ | } | ||
+ | ?> | ||
+ | |||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | |||
+ | <table border=" | ||
+ | < | ||
+ | <form action="<? | ||
+ | <tr> | ||
+ | < | ||
+ | < | ||
+ | </tr> | ||
+ | </ | ||
+ | <tr align=" | ||
+ | <td colspan=" | ||
+ | <font size=" | ||
+ | </td> | ||
+ | </tr> | ||
+ | </ | ||
+ | |||
+ | <?php | ||
+ | $cmd = NULL; | ||
+ | if (isset($_POST[' | ||
+ | $cmd = $_POST[' | ||
+ | } | ||
+ | if (isset($_POST[' | ||
+ | $pos = strpos($cmd," | ||
+ | if ($pos == NULL) { | ||
+ | $fifo_cmd = ": | ||
+ | } else { | ||
+ | $t1 = substr($cmd, | ||
+ | $t2 = substr($cmd, | ||
+ | $fifo_cmd = ": | ||
+ | } | ||
+ | |||
+ | $fifo_out=write2fifo($fifo_cmd, | ||
+ | |||
+ | if ($err) { | ||
+ | $errors=array_merge($errors, | ||
+ | echo "Error 1: $errors"; | ||
+ | die(); | ||
+ | } | ||
+ | |||
+ | if (substr($status, | ||
+ | $errors[]=$status; | ||
+ | echo "Error 2: $status"; | ||
+ | die(); | ||
+ | } | ||
+ | echo "< | ||
+ | echo " | ||
+ | } | ||
+ | ?> | ||
+ | |||
+ | </ | ||
+ | </ | ||
+ | </ |