Using Perl To Submit a Form
When you need to submit a form to another script without human intervention, this article can get you started. You'll find a browser-emulation script that will submit form field information (that you specify) automatically.
Reasons for doing so vary. One reason could be a form that needs to send email to the site owner and also submit part of the form information to another script, maybe a script that processes subscription requests. Another reason could be a cron-initiated script that consults a database and submits certain information to various form handling scripts around the 'net.
Often, I find I need my scripts to send information to another script with method="POST". This article provides a browser-emulation script with which I accomplish that.
Unedited, the browser-emulation script will post to a CGI script on one of our domains. It dumps the information it receives to your browser so you can see what was POSTed. (The dump script won't be on our domain indefinitely. Therefore, at the end of this article is one you can use for yourself.)
In this browser-emulation script, you're able to specify the URL to post the information to, the form field information to submit and, optionally, a name for your emulated browser:
#!/usr/bin/perl # Script to emulate a browser for posting to a # CGI program with method="POST". # Specify the URL of the page to post to. my $URLtoPostTo = "http://flowto.info/cgi-bin/Dump.cgi"; # Specify the information to post, the form field name on # the left of the => symbol and the value on the right. my %Fields = ( "name" => "Will Bontrager", "email" => "name\@example.com", ); # As seen above, "@" must be escaped when quoted. # If you want to specify a browser name, # do so between the quotation marks. # Otherwise, nothing between the quotes. my $BrowserName = "This Be Mine"; # It's a good habit to always use the strict module. use strict; # Modules with routines for making the browser. use LWP::UserAgent; use HTTP::Request::Common; # Create the browser that will post the information. my $Browser = new LWP::UserAgent; # Insert the browser name, if specified. if($BrowserName) { $Browser->agent($BrowserName); } # Post the information to the CGI program. my $Page = $Browser->request(POST $URLtoPostTo,\%Fields); # Print the returned page (or an error message). print "Content-type: text/html\n\n"; if ($Page->is_success) { print $Page->content; } else { print $Page->message; } # end of script
Verify the first line of the above script points to the location of perl on your server. Then, the script can be uploaded to try it out. Later, you can customize it.
-
Edit the script with a plain text word processor like NotePad or TexEdit.
-
Upload into a directory on your server allowed to run perl CGI scripts.
-
Give the script 755 permissions.
-
Type the URL of the script into your browser.
It's a fairly simple script. Not counting blank lines and comments, the script contains only 16 lines of code.
And it's easy to use. Just specify the URL of the form processing script, the form field names and values to be posted to the form processing script and, optionally, the name of the emulated browser. That's it.
The above script can be used as a base for more elaborate implementations.
Okay, I promised to provide a dump script:
#!/usr/bin/perl # Script to reflect the information POSTed to it. # It's a good habit to always use the strict module. use strict; # Put the information sent to the script # into the variable $queryString my $queryString = ''; read(STDIN,$queryString,$ENV{CONTENT_LENGTH}); # Start the web page. print "Content-type: text/html\n\n<html><body><pre>"; # Split the variable $queryString into name-value # pairs and print each pair. for( split(/&/,$queryString) ) { $_ =~ tr/+/ /; my ($n,$v) = split(/=/,$_,2); $n =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg; $v =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg; print "$n=$v\n"; } # End the web page. print '</pre></body></html>'; # end of script
The above script receives information POSTed to it and dumps it as a web page to the browser. In that way, you can know exactly what the dump script receives while you're fine-tuning your browser-emulation script.
Will Bontrager