Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Perl Socket (to mimic PHP code)

by inblosam (Monk)
on Jun 07, 2006 at 15:01 UTC ( [id://554041]=perlquestion: print w/replies, xml ) Need Help??

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

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.


Michael

Replies are listed 'Best First'.
Re: Perl Socket (to mimic PHP code)
by gellyfish (Monsignor) on Jun 07, 2006 at 15:19 UTC

    I'm not quite sure why you insist on mimicking the broken PHP way of doing this when you can do this simply with LWP::UserAgent:

    use LWP::UserAgent; use HTTP::Request; + # ... do your $XML + my $age = LWP::UserAgent->new(); $age->agent('IVR API'); + my $req = HTTP::Request->new(POST => 'http://api.voiceshot.com/ivrapi. +asp'); $req->content_type('text/xml'); $req->content($XML); + my $response = $age->request($req); + print $response->content();

    /J\

      Fantastic! That works like a charm!


      Michael
Re: Perl Socket (to mimic PHP code)
by Joost (Canon) on Jun 07, 2006 at 15:21 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://554041]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-24 18:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found