Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP - EDI Could/Should I use PHP for an EDI application?

Status
Not open for further replies.

southbeach

Programmer
Jan 22, 2008
879
US
Could/Should I use PHP to develop an EDI application? If so, where do I start? If not, what should I be looking at?

Thanks!


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
I would start by learning about PHP sockets. I assume that it will be a TCP/IP interface.

Next you'll need split, explode, and implode functions as well as understanding arrays.

You may want to write to a database. I use MySQL.

Here's a short example of an app that opens a socket, reads the input and writes to a MySQL database. Then it (should) send an acknowledgment message back through the same port...

Code:
#!/usr/bin/php -q
<?php
set_time_limit (0);

$host = "localhost";
$port = 1234;

$link = mysql_connect('localhost', 'username', 'password') or die('Could not connect: ' . mysql_error());
mysql_select_db('interface') or die('Could not select database');

$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");

while (false !== ($input = socket_read($spawn, 65536, PHP_BINARY_READ))){
$dtnow = date("Y-m-d H:i:s");
$pipe_parse = explode("|", $input);
$carat_msh9 = explode("^", $pipe_parse[8]);

$input = str_replace('\'','\'',  $input);
$input = str_replace('"','\\"',  $input);
$input = str_replace(',','\\,',  $input);

$query = "INSERT INTO in_msg (in_id, in_msg, in_event, in_event_type, in_datetime, in_read) VALUES(NULL, '$input', '$carat_msh9[0]', '$carat_msh9[1]', '$dtnow', '')";
$result = mysql_query($query) or die( mysql_error());

$output = "MSH|^~\&||$pipe_parse[3]|||$pipe_parse[6]||ACK|$pipe_parse[9]|$pipe_parse[10]|$pipe_parse[11]\rMSA|AA|$pipe_parse[9]";
socket_write($spawn, $output , strlen ($output)) or die("Could not write output\n");
}
socket_close($socket);
?>

I think it worked. Haven't played with this code for a while.

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top