I am trying to send some XML code through HTTP to a server and get the response. The example code they give is in PHP but I would rather use Perl. However, I am having a hard time figuring out how to mimic the PHP code, which opens a socket. Here is the PHP code, and then below is the start of some Perl code that doesn't do anything at this point.
<?php
$XML = '<somexml></somexml>';
// 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;
}
?>
Here is the perl code now. I can't figure out where the "ivrapi.asp" has to go, nor where to slide in the XML code through the socket to the server. When I run the code below it just sits, nothing back, but keeps running.
#!/usr/bin/perl -w
use strict;
use Socket;
my ($remote,$port, $iaddr, $paddr, $proto, $line);
$remote = 'api.voiceshot.com';
$port = 80;
$iaddr = inet_aton($remote) || die "no host: $remote";
$paddr = sockaddr_in($port, $iaddr);
$proto = getprotobyname('tcp');
socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
connect(SOCK, $paddr) || die "connect: $!";
while (defined($line = <SOCK>)) {
print $line;
}
close (SOCK) || die "close: $!";
exit;
I have looked at IO::Socket::INET but it seems to have just the same components as the Socket module in the code above. The
perlipc page gave me the example code using Socket. And if its helpful, the server is not my server so I only have the client to work with.