[SWLUG] perl cgi advice

Dafydd Walters dafydd at walters.net
Wed May 14 22:50:56 UTC 2003


Gareth,

This is fairly straightforward using the LWP::UserAgent module.

Your CGI script would look something like this (this assumes the target CGI
script handles form data via the POST method):
-----------------------------------------------------

#!/usr/bin/perl

use LWP::UserAgent;
use URI::Escape;

print "content-type: text/html\n\n";
print "<html><head><title>LWP::UserAgent test</title></head><body>\n";

# Construct a query body containing any POST
# variables to be sent to the foreign script
$query = "post_param1=" . uri_escape("value of param 1");
$query .= "&post_param2=" . uri_escape("value of param 2");
$query .= "&post_param3=" . uri_escape("value of param 3");

# Create user agent object
$ua = new LWP::UserAgent;

# Request
$req = new HTTP::Request
'POST','http://foreign_site.com/foreign_script.cgi';
$req->content_type('application/x-www-form-urlencoded');
$req->content($query);
$result = $ua->request($req);

# result is in $result
if ($result->is_error) {
    print "<p>HTTP error</p>\n";
} else {
    print "<p>Result text is in the variable
<b>\$result->content</b></p>\n";
}

print "</body></html>\n";

-----------------------------------------------------



----- Original Message -----
From: "Gareth Lewis" <Gareth.Lewis at patent.gov.uk>
To: <discuss at swlug.org.uk>
Sent: Wednesday, May 14, 2003 4:17 AM
Subject: [SWLUG] perl cgi advice


> Hi All,
>
> I have an idea for a web site that will involve me wanting to do the
following :
>
> in response to a GET,  my PERL CGI script will need to try and run a
external CGI script on anothers site and then process the results.
>  IE  i want my script to do a look up on a third parties web server. (its
essentially a form of dictionary lookup on the third site)
>
> I assume that this is fairly easy for perl ?
>
> I am a newbie on this sort of stuff so any advice that can avoid days of
confusion would be great !
>
> I have a perl cgi book that I'll try reading again.
>
> if anyone can point me to any simple & short scripts that do similar
things for me to study, that would be ideal.
>
> cheers,
>
> Gareth.
>
>
>
>
>
> _______________________________________________
> SWLUG Discussion List - Discuss at swlug.org.uk
> http://swlug.org.uk/mailman/listinfo/discuss
>





More information about the Swlug mailing list