[thelist] php and http form post requests

Dan Hardison Dan.Hardison at jegs.com
Thu Mar 13 07:51:41 CST 2003


I'm attempting to submit an .xml file via an http post request using
php.  I receive the following response/error from my php code.

HEADER ::
POST /sg.xml HTTP/1.0 Host: 192.168.100.246 Content-type: text/xml
Content-length: 523 01 00 tpi RESPONSE ::
HTTP/1.1 200 OK Date: Thu, 13 Mar 2003 13:44:35 GMT Server:
Apache/1.3.27 (Unix) PHP/4.3.1 mod_perl/1.27 Connection: close
Content-Type: text/html Not Determined XML Parser Error at line number 1
not well-formed XML Syntax Error Thu Mar 13 08:44:35 AM

--

The php code I'm currently trying is as follows:

----------
<?php
// a file contain post data
$file = "search.xml";
$remote_url = "/sg.xml";
$remote_server = "192.168.100.246";
// read post data
$data = file($file);

// built up POST data
$request = "";
for ($i=0;$i<count($data);$i++)
{
  $request .= $data[$i];
}


// Build the header
$header = "POST $remote_url HTTP/1.0\r\n";
$header .= "Host: $remote_server\r\n";
$header .= "Content-type: text/xml\r\n";
// $header .= "Content-type: application/x-www-form-urlencoded\r\n";
$header .= "Content-length: " . strlen($request) . "\r\n\r\n";
// Open the connection
$fp = fsockopen($remote_server, 80);
if ($fp)
{
// Send HTTP request
fputs($fp, $header . $request);
print "HEADER :: <br/>" . $header . $request;
// Get the response
$response="";
while (!feof($fp))
$response .= fgets($fp, 128);
fclose($fp);
print "RESPONSE :: <br />" . $response;
}
else
die ("Cannot connect !!!");
?>
----------

My .xml file (search.xml) is as follows:

----------
<?xml version="1.0"?>
<!-- SHOPPING REQUEST -->
<tt0003 id="tt0003">
<!-- THIS IS THE ACTUAL DATA REQUIRED FOR TRANSACTION COMPLETION -->
<sg_data_to_macs>
<COMPANY>01</COMPANY>
<DIVISION>00</DIVISION>
<UID></UID>
<SEARCH_KEY_WORD>tpi</SEARCH_KEY_WORD>
<CURR_PAGE_NUM></CURR_PAGE_NUM>
<PAGE_LEN></PAGE_LEN>
<CAT_PROD_NUM></CAT_PROD_NUM>
</sg_data_to_macs>
<!-- THIS IS ANY ADDITIONAL TRANSACTION DATA -->
<user_data_for_return_with_response>
<a></a>
<z></z>
</user_data_for_return_with_response>
</tt0003>
----------

If I submit the previous xml data (search.xml) as a true html form post
(see code below - basically I'm pasting the xml code in a textarea) it
works great.  

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>XML Testing page</title>
</head>
<body>
<center>
<form method="post" action="http://192.168.100.246/sg.xml"
autocomplete=off>
<input type="submit" value="SUBMIT"><BR>
<textarea wrap=virtual rows=35 cols=80 name="d">
</textarea></form></center>
</body>
</html>

-------------

Any help would be GREATLY appreciated.

TIA,
Dan


More information about the thelist mailing list