jdtoronto has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed monks,

I have the enviable task of replacing some PHP code with Perl code. The PHP code was written to submit some XML to a remote server. Here it is:

<?php $XML = "<campaign action=\"6\" />"; if ($_POST['submit'] == 'view') { header("Content-type: text/xml"); echo $XML; } else { // Post header $VS_header = "POST /ivrapi.asp HTTP/1.0\r\n"; $VS_header .= "User-Agent: IVR API\r\n"; $VS_header .= "Host: api.voiceshot.com\r\n"; $VS_header .= "Content-Type: text/xml\r\n"; $VS_header .= "Content-length: ".strlen($XML)."\r\n"; $VS_header .= "Connection: close\r\n\r\n"; $stream = fsockopen("api.voiceshot.com", 80); if($stream) { fputs($stream, $VS_header); fputs($stream, $XML); $response=""; $header = ""; // code for parsing out the returned header do $header.=fread($stream,1); while (!preg_match('/\r\n\r\n$/',$header)); // Everything left is the XML response from the API while(!feof($stream)) $response .= fgets($stream, 1024); fclose($stream); header("Content-type: text/xml"); echo $response; } } ?>
All of this XML stuff is very new to me, but I assumed I could do this as an equivalent:
sub _vs_connectivity_check { my $self = shift; use LWP::UserAgent; my $ua = LWP::UserAgent->new; my $req = HTTP::Request->new(POST => 'http://api.voiceshot.com/ivrap +i.asp'); $req->content_type("text/xml"); $req->content( '<campaign action="6">'); my $res = $ua->request($req); print $res->as_string; }
But, by way of exceeding enlightenment I receive this from the server:
HTTP/1.1 200 OK Cache-Control: private Connection: close Date: Tue, 12 Jul 2005 19:27:30 GMT Server: Microsoft-IIS/5.0 Content-Length: 87 Content-Type: text/html Client-Date: Tue, 12 Jul 2005 19:34:08 GMT Client-Peer: 64.105.135.234:80 Client-Response-Num: 1 Set-Cookie: ASPSESSIONIDACCABCRB=LPAGGBNCGIBHEMOHFDCAAMLJ; path=/ <?xml version="1.0"?><campaign errorid="3" comment="XML file malformed + or missing." />
Which leads me to believe that maybe I have an escaping or encoding issue? Can anybody help me please?

jdtoronto

Replies are listed 'Best First'.
Re: A newbie XML and HTTP question.
by bmann (Priest) on Jul 12, 2005 at 20:04 UTC
    jdtoronto,

    The XML submitted by perl is not well-formed, hence the "malformed" error message.

    It looks to me that all you need to do is close the 'campaign' element, ie <campaign action="6" /> (note the ' />').

      Exactly! I sat here and looked at it again for the umpteenth time in as many hours - and suddenly it dawned on me - now it works perfectly.

      Thanks!

        xmllint from the libxml2 project is really good for trouble-shooting possibly bad XML. I use it frequently when working on Petal templates which must be well-formed XML.

        -sam