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

I have a question about using Perl's LWP::UserAgent. I've played around with it enough to understand how to return the source code from a page but now I want to learn how to actually submit form fields using Perl. However after trying quite a few examples I still cannot get it to work. Just for trying purposes I decided to use this site www.wheresmycellphone.com Here is my current Code
#!/usr/bin/perl -w use strict; use LWP::UserAgent; use HTTP::Request::Common qw(POST); my $browser = LWP::UserAgent->new; my $resp = (POST 'www . wheresmycellphone . com/', ['txtisdcode' => 1, + txtcitycode' => '000', 'txtphone' => '0000000']); my $response_to_discard = $browser->request($resp); print $response_to_discard;

It runs without errors but I expect to receive a call on my cell phone which I never do. Any ideas on what I'm doing wrong?


- John

Replies are listed 'Best First'.
Re: Perl LWP and POST
by almut (Canon) on May 31, 2009 at 02:55 UTC

    Maybe try something like this instead:

    #!/usr/bin/perl -w use strict; use LWP::UserAgent; my $browser = LWP::UserAgent->new; my @req = ('http://www.wheresmycellphone.com/submit.php', [txtisdcode => 1, txtcitycode => '000', txtphone => '00000 +00']); my $response_to_discard = $browser->post(@req); print $response_to_discard->as_string();

    In particular, that line

    my $resp = (POST 'www . wheresmycellphone . com/', ['txtisdcode' => 1, + txtcitycode' => '000', 'txtphone' => '0000000']);

    is wrong in several respects:

    • POST is a bareword
    • ...that is not separated from the next item with a comma
    • txtcitycode' should either be quoted properly, or not at all ('=>' already does that)
    • $resp should be @resp, otherwise you'd only have the final arrayref of that list in $resp
    • the URL must be absolute (http://...)
    • the respective form on www.whereismycellphone.com is posting to submit.php, not /

    (I wonder how you got that past -w and use strict;...)

      Thanks! This works alot better. After looking at the HTML for the page it is what I expect. However, I still don't receive the phone call. It is as though the data I am trying to submit isn't actually being submitted. Any idea why this is?

        Have you tried adding submitcall => "Make it Ring!", as atcroft suggested?

        If that doesn't work either, I'd submit the form from a browser, while logging the headers that are being exchanged — using Firefox's LiveHTTP Headers add-on, or some such, in order to see if there's something else (session cookie?) that you need to mimic...

Re: Perl LWP and POST
by atcroft (Abbot) on May 31, 2009 at 02:49 UTC

    Quick observation: the form on that site contains a "submitcall" field which appears to be set to "Make it Ring!", which your example code does not include. Does it still fail if you include that field?

    HTH.