![]() |
|
06.01.10 A Whole New World (Wide Web) By
Michael Marr
Although Perl may not be able to produce flying carpets, blue genies, or beautiful Arab princesses, it can be used for nearly limitless other applications. One of Perl's greatest strengths is playing nice with other programming languages, and thus why it may be valuable to experiment with wrapping popular APIs into a Perl script that can be used by the application(s) of your choice. You won't need a charming monkey sidekick to access APIs with Perl. Instead, you'll need the nice little LWP module. Using this module, we're able to rub the lamp and connect to the nearly limitless possibilities of the World Wide Web (there are some rules limiting your World Wide Web requests, i.e. although many sites on the web would disagree with this, the Web can't make someone fall in love with you). Instead, for many APIs on the web, you will need to authenticate your request. This can often be done via the following call:my $ua = LWP::UserAgent->new; $ua->credentials('api-host.website.com:port', 'Some Realm/API Keyword', 'username', 'password'); Using the above code will setup HTTP authentication compliant with RFC 2616, and is often required when accessing a public API. The second and/or alternative authentication step you may run into is adding special headers to your request: use HTTP:Headers; $ua->default_headers->push_header('Header Field' =>'special value'); After getting your all your "open sesame"s in place, you're ready to push out your request. Normally, this will be accomplished by building an appropriate URI and pushing it out the API. Once you have sent your HTTP request, you wait for the matching response, and handle accordingly. my $response = $ua->post('http://api-host.website.com/api_request/get_data.xml'); Many APIs give you options on the return response, whether it be in XML, JSON, or other format. Once you have your response in the format of your choice, use an appropriate Perl module to manipulate your data, and fully wield the power of the true LAMP(erl). Here's one short example utilizing Twitter's API: #! /usr/bin/perl -w # demo of accessing an API (Twitter, in this case) with Perl # see more on Twitter's API: http://apiwiki.twitter.com/ my $username = 'mikemarr33'; my $password = 'topsecretz'; use LWP; use HTTP::Headers; my $ua = LWP::UserAgent->new; $ua->credentials('twitter.com:80', 'Twitter API', $username, $password); # we don't need any special headers here - Twitter's API does not require it. # $ua->default_headers->push_header(); # we are using the HTTP request method get instead of post, as this # API request requires my $response = $ua->get('http://twitter.com/statuses/friends.xml'); # here, we could manipulate the response however we desire, but for # conciseness and demonstration purposes, we'll print to see that # we're getting relevant information print $response->content; Although some may say that you always have been and always will be a street rat, I am optimistic that you can use the proper tools to get out and be productive in that big World Wide Web.
|
||||||||||
|
| ||
|