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

Hello Gurus, I'm trying to download an mp3 using the LWP module and then save it in a file.But every time I open up the file it's all garbled and the sound is screwed up. Another thing is when I type the url directly in my browser it prompts me to save the file which it says is a php file but when I open it in Windows Media player it will still play it perfectly, so maybe that weirdness has something to do with my problem. Hopefully someone can point me in the correct direction.I've tried using different types of content-types including the type that my browser uses when getting that file. I can post the code that I'm using on request. Thanks, j3f

Replies are listed 'Best First'.
Re: using LWP to download and save an mp3
by Corion (Patriarch) on Jan 16, 2009 at 09:08 UTC

    Consider this the request to post your code. You don't show how you save the file. I recommend using the ->mirror() function of LWP::Simple to save files. Most likely you're missing a binmode call on the filehandle you're saving to.

      Here is the code that shows the way I was trying to save the file.I'll try using the function that you suggested and see if it solves my problem and I'm guessing that it will. Thanks
      #!/usr/bin/perl -w use LWP::UserAgent; $ua = LWP::UserAgent->new; $ua->agent('Mozilla/5.0'); my $req = HTTP::Request->new(GET => 'http://sight.com/action.php?id=u1 +VInM4D'); $req->content_type('audio/x-mpeg'); # Pass request to the user agent and get a response back my $res = $ua->request($req); # Check the outcome of the response if ($res->is_success) { open FH, ">file.mp3"; print FH $res->content; close FH; }