[thelist] php and http post requests

Roger Ly rogerly at bareviking.com
Thu Dec 26 21:36:01 CST 2002


Hey Tom,

Let me modify my existing post...

So, it seems you need the Content-type to be text/xml and there are some other
things that I missed by not testing the code before sending it off...

Anyways, this seems to work:


==========
<?php
// a file contain post data
$file = "test.xml";
$remote_url = "/xte/storesense/Process.bok";
$remote_server = "www.silverheaven.com";
// 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-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 !!!");
?>
======
So, I did have to recreate the request object by reading in from the file a line
at a time (when I passed along just the $data object, it just passed the array
that held the content of the file read in (d'oh!).  Also, like I said, I also
changed the Content-type to text/xml since the other one was one that was used
for form data (which in this case, we are not dealing with).

I did post some sample data to your StoreSense installation (sorry, I donn't
have our internal one to play with right now... hope you don't mind...)

Again, let me know if that works (testing against your server with your test.xml
file yielded an error from StoreSense, but that was basically due to invalid
data being passed in the XML (the error message details what is missing from
your XML).

HTH,

Roger



More information about the thelist mailing list