[thelist] php and http post requests

Tom Dell'Aringa pixelmech at yahoo.com
Thu Dec 26 13:20:00 CST 2002


--- Lauri Vain <lauri_lists at tharapita.com> wrote:
> Try:
> http://www.phpbuilder.com/tips/item.php?id=239

Okay, I found a script block there that seems to get me 80% to where
I need to be. I plugged in my info into this script:

-------------------------------------------------------------
<?php
// function to read server name
function servername($txt)
{
  if (substr(strtoupper($txt),0,4)=="WWW.")
     $txt="HTTP://".$txt;
  if (substr(strtoupper($txt),0,7)!="HTTP://")
      return 0;
  eregi("^(http://([^/ ]+))",$txt,$arr);
  return $arr[2];
}

// a file contain post data
$file = "test.xml";
$remote_url = "http://www.mydomain.com/xte/storesense/Process.bok";

// read post data
$data = file($file);

// built up POST data
$request = "?";
for ($i=0;$i<count($data);$i++)
    {
    $arr = explode("=",$data[$i]);
    if (count($arr)!=2) continue;
    $request .= urlencode(trim($arr[0])) .
                "=" .
                urlencode(trim($arr[1])) . "&";
    }

// Build the header
$header = "POST $remote_url HTTP/1.0\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(servername($remote_url), 80);

if ($fp)
  {
   // Send HTTP request
   fputs($fp, $header . $request);

   // Get the response
   $response="";
   while (!feof($fp))
   $response .= fgets($fp, 128);

   fclose($fp);

   print "RESPONSE :: <br />" . $response;
   }
else
   die ("Cannot connect !!!");

?>
-------------------------------------------------------------

Now, I get a 302 and I get the info:

"HTTP/1.1 302 Found Date: Thu, 26 Dec 2002 19:01:19 GMT Server:
Apache/1.3.20 Sun Cobalt (Unix) Chili!Soft-ASP/3.6.2 mod_jk/1.1.0
mod_ssl/2.8.4 OpenSSL/0.9.6g PHP/4.1.2 mod_auth_pam_external/0.1
FrontPage/4.0.4.3 mod_perl/1.25 Location:
http://www.silverheaven.com/xte/storesense/Process.bok Connection:
close Content-Type: text/html; charset=iso-8859-1 "

Returned to me. You can see this by going to

http://www.silverheaven.com/reg/post.php

-- But this isn't quite what I need. It's nice that I got a response
back but the Process.bok didn't handle the xml transaction, it only
sent back a response from the page. Now the documentation says I need
to do this:

"The XML stream should be sent to http://<your
ipaddress>/xte/storesense/Process.bok. The stream is sent over HTTP
via a POST"

I thought that was what I did here..but maybe not. Now on the post
page I put up, there is a line that says

The document has moved here. (here being a link)

Clicking that gets you what kind of SHOULD happen, except via GET
instead (of course you see the error in the xml response.)

Is my XML file (test.xml) not getting to that url I specified in my
function above? ($remote_url =
"http://www.mydomain.com/xte/storesense/Process.bok";)

Tom


=====
var me = tom.pixelmech.webDeveloper();

http://www.pixelmech.com/
http://www.maccaws.com/
[Making A Commercial Case for Adopting Web Standards]

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com



More information about the thelist mailing list