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

#!/fellow/monks.pl I found the following code on http://timjoh.com/perl-post-automatically-to-wordpress-blog-with-wp-posterpl/ which will allow a user to publish information into a Wordpress blog. From what I can tell, the code is fine. The problem is when the the authentication bit starts, Wordpress issues a cookie, or at least tries to.

The problem is Wordpress comes back saying the browser doesn't support cookies.

Correct me if I'm wrong, but $ua->cookie_jar( {} ); is the cookie jar, right? Why doesn't LWP tell Wordpress the right thing? Is it something else?

use strict; use LWP::UserAgent; # ====== Parameters to change =============== my $burl = 'http://www.example.com/'; # don't forget the trailing slas +h my ( $usr, $pwd, $uid ) = ( 'admin', 'secret', 1 ); # uid=1 if you are + the initial administrator my $title = "Wordpress Post title"; my $body = "Wordpress body text"; # =========================================== # ua my $ua = LWP::UserAgent->new; #$ua->agent('wp-poster'); $ua->cookie_jar({}); # login print "Login : "; my $req = HTTP::Request->new( POST => $burl . 'wp-login.php' ); $req->content_type('application/x-www-form-urlencoded'); $req->content( sprintf('log=%s&pwd=%s&wp-submit=1&redirect_to=wp-admin +/post-new.php',$usr,$pwd) ); my $res = $ua->request( $req ); if ( $res->is_success ) { print "SUCCESS!!\n"; print $res->content; ##### Yes, we're successfull, but Wordpress says cookies aren' +t enabled... Huh ?? } else { print "FAILURE!!\n"; &debug($res, 0); exit(1); }

Thanks!

Massyn

Replies are listed 'Best First'.
Re: LWP ignoring cookies??
by TOD (Friar) on Jan 05, 2008 at 05:17 UTC
    actually i have not much practice with the LWP project, but from my distant point of view you might receive the cookie when you omit the line $ua->cookie_jar({});. according to the pod from LWP::UserAgent the field is initially undefined. so from my understanding explicitly setting it to an empty hash reference overrides the default behaviour and will result in an attempt to send an empty cookie together with the request - which is, however, not conforming to http standards.
    --------------------------------
    masses are the opiate for religion.
      Nope, still the same. Even trying the script on Win32 (Activestate Perl) does the same.
        Ok, this worked...
        my $ua = LWP::UserAgent->new( cookie_jar =>HTTP::Cookies->new( file => + '/tmp/cookies.txt', autosave => 1, ignore_discard => 1 ));
        and then I had to make sure that /tmp/cookies.txt exists, and that it's accessable. Silly though - I would have expected the in-memory cookie jar to operate...