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

I am writing a perl script, that needs to read an xml response from twitter, and I would like to use curl and LWP. The Twitter REST call requires a Url as well as a user name and a password. I am having trouble making this work:
my $Url="".$twitterID.".xml"; my $username="something"; my $pwd="pwdSomething"; my $curl = LWP::UserAgent->new; $curl->setopt(CURLOPT_CONNECTTIMEOUT, 2); $curl->setopt(CURLOPT_HEADER, false); $curl->setopt(CURLOPT_HTTPAUTH, CURLAUTH_BASIC); $curl->setopt(CURLOPT_RETURNTRANSFER, 1); $curl->setopt(CURLOPT_URL, $Url); $curl->setopt(CURLOPT_USERPWD, "$username:$pwd"); $curl->setopt(CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); $curl->setopt( CURLOPT_POST, 1 ); my $xmlResponse = $curl->perform;

Replies are listed 'Best First'.
Re: How do I use curl with perl and Twitter
by Corion (Pope) on Jul 20, 2009 at 19:38 UTC

    Why don't you use Net::Twitter, which is a tested and working Perl interface to the Twitter API? The ->show_user() method seems to be applicable.

      So, then if I use something like this, it should work?
      my $nt = Net::Twitter->new( traits => [qw/API::REST/], username => $username, password => $pwd, apihost => "/users/show/".$TwitterID.".xml" ); my $result = $nt->show_user;
      The API url defaults to, and I assume that the apihost, would be the remainder of the REST call, and by calling show_user, instead of show_user(id) -- while using an apihost that terminates with .xml, I am guaranteed a response that's an xml string. The documentation was not clear on what format I should expect if I use show_user(id), which would assume the default apihost.

        I'm not sure that what you wrote above would work, because the apihost parameter is, unsurprisingly, documented in the Net::Twitter as:

        A string containing the Twitter API host. It defaults to "". This option is available when the API::REST trait is included.

        I'm not sure where you get the idea that you have to construct any URLs yourself - this is what the module should shield you from. But I'm sure that, instead of theoretical musings, it would be far more productive if you actually tried things out and came back only if you had further questions or error messages to report.

        You're making this far harder than it needs to be.

        #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Net::Twitter; my $nt = Net::Twitter->new; print Dumper $nt->show_user('davorg');

        You don't even need to authenticate unless you're trying to access a user who has protected their updates.


        See the Copyright notice on my home node.

        Perl training courses

      Ordinarily I'd agree with you 100%. But playing devil's advocate for a second means I should point out that Net::Twitter requires Moose which requires half the CPAN. So one possible reason why anyone would avoid Net::Twitter is the somewhat lengthy and involved install process.

      But in that case there's Net::Twitter::Lite

      -- vek --
        Net::Twitter requires Moose which requires half the CPAN

        Actually it has only 4 direct (non-core) dependencies, this expands to only 21 dependencies in total (6 of which are core). See here for the actual list of dependencies, and here for a detailed listing and justification for each one.

Re: How do I use curl with perl and Twitter
by davorg (Chancellor) on Jul 20, 2009 at 20:54 UTC
Re: How do I use curl with perl and Twitter
by psini (Deacon) on Jul 20, 2009 at 19:39 UTC

    Please format your code if you hope someone will read it.

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."