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

Hello,

I'm stuck.

I have a perl script which does loads of cool and magical things. Basically it's an alert system which send out an email when things have gone wrong. It runs on a cron every hour. We now need to make it send out text messages, and i've found a solution here: http://www.clickatell.com which will let us do this. We just need to send a http request and the message will be sent.

So I need to get my script to send a request which looks like this:

http://api.clickatell.com/http/sendmsg?api_id=APIID&user=me&password=p +assword&to=mynumber&text=Hello+me

My question is how can I do this? If it was a web application this would be easy, i'd just open up a new window and tell it to go to that location, maybe using javascript or something. But this script is running on a cron every hour.

I think I might be being stupid here and the answer is quite simple, but I just don't know how to do it.

Any help or advice would be appreciated.

Thanks,

Tom

Learning without thought is labor lost; thought without learning is perilous. - Confucius
WebChalkboard.com | For the love of art...

Replies are listed 'Best First'.
Re: Sending a http request from a cron script
by tlm (Prior) on Mar 29, 2005 at 10:41 UTC

    LWP is your friend:

    use LWP::Simple; my $html = get( 'http://api.clickatell.com/http/sendmsg?api_id=APIID&u +ser=me&password=password&to +=mynumber&text=Hello+me' );
    Try that. If it fails, post the error message. (You may need to fiddle with the user agent string sent by the request, but we'll worry about this if you need it.)

    the lowliest monk

Re: Sending a http request from a cron script
by polettix (Vicar) on Mar 29, 2005 at 10:41 UTC
    You can peruse LWP and Mechanize, which will probably serve you very well. BTW, if you only want to send a simple HTTP GET, try looking at LWP::Simple first.

    Flavio

    Don't fool yourself.
Re: Sending a http request from a cron script
by Zaxo (Archbishop) on Mar 29, 2005 at 10:44 UTC

    Cron automatically mails anything on STDERR to the address in the MAILTO environment variable. You can set that at the top of your crontab, or else in the command line you give the crontab for your perl script. Then if things go wrong, just say, warn $msg if $condition; in your script and the news is in the mail.

    To answer your question anyway, if you need to make a request to some webserver in a script, cronned or not, LWP::UserAgent is the classic choice.

    After Compline,
    Zaxo

      Even if the MAILTO line is not there, cron still mails to the owner of the script it executes.


      Manav
Re: Sending a http request from a cron script
by webchalkboard (Scribe) on Mar 29, 2005 at 10:40 UTC

    As is so often the way, as soon as I sent this message I came up with a solution :)

    I haven't tested it yet, but would it work to just use LWP::UserAgent to send out a request?

    Learning without thought is labor lost; thought without learning is perilous. - Confucius
    WebChalkboard.com | For the love of art...
Re: Sending a http request from a cron script
by Frantz (Monk) on Mar 29, 2005 at 10:45 UTC
    There are many solution: You can use tools like wget or curl,

    or use perl module LWP to build an agent en call it from cron.

    I think the simpliest solution is to use wget :
    http://www.gnu.org/software/wget/wget.html

      No need to 'build an agent', for LWP comes with lwp-request and will ask you to install the aliases GET, HEAD, and POST. If you use those aliases, a simple `GET $url` will be sufficient.

      --
      b10m

      All code is usually tested, but rarely trusted.
Re: Sending a http request from a cron script
by InfiniteLoop (Hermit) on Mar 29, 2005 at 10:43 UTC
    LWP module should solve your problem.
Re: Sending a http request from a cron script
by PodMaster (Abbot) on Mar 29, 2005 at 10:45 UTC