Reading HTTP POST

Hello,

Here is the situation, a service will send some data to my web server using HTTP POST method. I need to catch that data and save it into a text file. The data is in XML format.

Anyone know how to do this in Perl ?

I got a PHP example below. It is working perfectly, but I need Perl version because I cant find a "good" way to read XML using PHP.

PHP example :

<?php

$xmlstring = '';

function getxml()
{
GLOBAL $xmlstring;

  $handle_i = fopen('php://input', "r");

  do
  {
    $xml = fread($handle_i, 2048);
    if (strlen($xml) == 0)
    {
      break;
    }

    $dummy .= $xml;

  } while (true);

  $dummy2 = urldecode($dummy);
  $xmlstring = str_replace("&", "", $dummy2);
  fclose($handle_i);
  return 0;
}

getxml();

$fname = "order.txt";
$fd = fopen($fname, "w");
fwrite($fd, $xmlstring);
fclose($fd);

?>


LVL 1
db2tradeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TintinCommented:
Can you give an example of the XML data that's sent in the HTTP POST?
0
ozoCommented:
I'm not sure what you're asking.  It sounds like what you need may be as simple as

use LWP::Simple;
print STDERR status_message getstore("php://input", "order.txt"),"\n";

perhaps also using XML::Dumper or XML::Simple


0
db2tradeAuthor Commented:
The server will send XML data for about 2 KB size if saved into a text file.

It is just a basic xml. The XML here is not the problem. What I am looking for is how to capture data that is sent from a server using HTTP POST.

SERVER_A will send data to SERVER_B using HTTP POST.

I only have a PHP script (see my first post), it is working fine, the data will be saved into a text file. Now I need a perl version if you could help me.
0
rugdogCommented:
I assume SERVER B will use a CGI to respond to the request? if so:

#!/usr/bin/perl
use CGI;

open(O,"> order.txt") or die "failed to open file: $!\n";
while(<STDIN>){
  print O CGI::unescapeHTML($_);
}
close(O);

print "content-type: text/html\n\n";
print "done";
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TintinCommented:
what does

php://input,r

do?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.