====== FIFO Client PHP Example ====== A sample implementation of a FIFO client in PHP you can integrate it in your web applications. OpenSER FIFO Client
OpenSER FIFO Client
Please do not enter the leading ":" or the trailing ":REPLY_FILE_NAME"
0)) { $host="192.168.1.1"; # IP Address of the OpenSER (FIFO) Server $port = 12345; # Port that OpenSER (FIFO) is listening on $pos = strpos($cmd,"\n"); if ($pos == NULL) { $fifo_cmd = ":$cmd:REPLY_FILE_NAME\n"; } else { $t1 = substr($cmd,0,$pos-1); $t2 = substr($cmd,$pos); $fifo_cmd = ":$t1:REPLY_FILE_NAME$t2\n"; } $timeout = 5; $fp = fsockopen ($host, $port, $errno, $errstr, $timeout); if (!$fp) { die("Error opening socket: $errno $errstr"); } else { echo "
FIFO Command:
$fifo_cmd

"; // write the user string to the socket fputs ($fp, $fifo_cmd); // get the result while ($result = fgets ($fp, 1024)) { echo "Response: $result
"; } // 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. fifo_server="/tmp/openser_fifo"; /* values used for names of reply fifos -- they change randomly this values shouldn't be changed unless you well know what are you doing */ $config->reply_fifo_filename="openserwebfifo_".rand(); $config->reply_fifo_path="/tmp/".$config->reply_fifo_filename; /* This function is taken from "serweb" */ function write2fifo($fifo_cmd, &$errors, &$status){ global $config; /* check if fifo is running */ if (!file_exists($config->fifo_server) or filetype($config->fifo_server)!="fifo"){ /*log_errors(PEAR::raiseError("FIFO not running or bad path to it", NULL, NULL, NULL, "fifo path:".$config->fifo_server), $errors);*/ echo "FIFO not running or bad path to it"; return false; } /* open fifo now */ $fifo_handle=fopen( $config->fifo_server, "w" ); if (!$fifo_handle) { $errors[]="sorry -- cannot open write fifo"; return false; } /* create fifo for replies */ @system("mkfifo -m 666 ".$config->reply_fifo_path ); /* add command separator */ $fifo_cmd=$fifo_cmd."\n"; /* write fifo command */ if (fwrite( $fifo_handle, $fifo_cmd)==-1) { @unlink($config->reply_fifo_path); @fclose($fifo_handle); $errors[]="sorry -- fifo writing error"; return false; } @fclose($fifo_handle); /* read output now */ @$fp = fopen( $config->reply_fifo_path, "r"); if (!$fp) { @unlink($config->reply_fifo_path); $errors[]="sorry -- reply fifo opening error"; return false; } $status=fgets($fp,256); if (!$status) { @unlink($config->reply_fifo_path); $errors[]="sorry -- reply fifo reading error"; return false; } $rd=""; while (!feof($fp)) { $rd.=fread($fp,8192); } @unlink($config->reply_fifo_path); return $rd; } ?> OpenSER FIFO Client
OpenSER FIFO Client
Please do not enter the leading ":" or the trailing ":REPLY_FILE_NAME"
0)) { $pos = strpos($cmd,"\n"); if ($pos == NULL) { $fifo_cmd = ":$cmd:".$config->reply_fifo_filename."\n"; } else { $t1 = substr($cmd,0,$pos-1); $t2 = substr($cmd,$pos); $fifo_cmd = ":$t1:".$config->reply_fifo_filename."$t2\n"; } $fifo_out=write2fifo($fifo_cmd, $err, $status); if ($err) { $errors=array_merge($errors, $err); echo "Error 1: $errors"; die(); } if (substr($status,0,1)!="2" and substr($status,0,3)!="404") { $errors[]=$status; echo "Error 2: $status"; die(); } echo "
FIFO Command:
$fifo_cmd

"; echo "Response: $fifo_out
"; } ?>