Post to phpbb using WWW::Mechanize from CPAN

Unfortunately there is no module on CPAN that supports posting to a phpbb blog, so I did it with WWW:Mechanize. While it looked like a simple task, I had a very frustrating time getting this to work. The reason is that phpbb will silently refuse to post if the script does so too fast. So the key to get it working is a sleep statement, like shown in the following sample code.

  my $mech = WWW::Mechanize->new();
  push @{ $mech->requests_redirectable }, 'POST';
  my $response;
  $response = $mech->get($bbUrl."/ucp.php?mode=login");
  $response = $mech->submit_form(with_fields=>{username=>$bbUser, password=>$bbPassword}, button=>"login");
  $response = $mech->get($bbUrl."/posting.php?mode=post&f=".$params{forumid});
  sleep(3); # workaround for phpbb's silly "antispam feature"
  $response = $mech->submit_form(with_fields=>{subject =>$params{subject}, message =>$params{message}}, button=>"post", form_name=>"postform");

The corresponding code in phpbb is the following, in posting.php:

// Was cancel pressed? If so then redirect to the appropriate page
if ($cancel || ($current_time - $lastclick < 2 && $submit))

Hard coded values, non-self-speaking code, lack of appropriate comment… The more I use phpbb, the more disappointed I become!

This entry was posted in Perl. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s