PerlでWeb API入門

  • 4,557 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
4,557
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
25
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Perl Web API 
 Web API Perl yusukebe 2010/07/23 Yokohama.pm#6 x PerlCasual#3
  • 2. •  Perl •  Web API &&Perl •  / –  LWP::* •  –  20 : Web API ← –  30 : sugyan •  ( )
  • 3. Web API •  API = Application Programming Interface –  / •  Web API API
  • 4. Web API •  Twitter API •  Google Ajax API •  YouTube API •  Flickr API •  mixi API •  API •  etc…
  • 5. / •  Web –  HTTP –  URI –  GET/POST –  JSON/XML etc.
  • 6. 3 (GET ) 1.  URI 2.  GET 3. 
  • 7. •  Twitter perl –  http://search.twitter.com/search.json?q=perl •  GET •  JSON • 
  • 8. Twitter Search API •  Twitter •  http://dev.twitter.com/doc/get/search • 
  • 9. API •  URI –  http://search.twitter.com/search.json •  –  JSON/atom •  –  GET •  –  q=perl
  • 10. Web
  • 11. URI •  ASCII •  URI •  http://search.twitter.com/search.json?q= %E3%81%8A%E3%81%AF %E3%82%88%E3%81%86
  • 12. URI use URI;  use utf8;  my $query  = ' ';  my $uri    =  URI‐>new('http://search.twitter.com/search.json');  my $params = {      q           => $query,      result_type => 'popular',  };  $uri‐>query_form(%$params);  print "$urin";  http://search.twitter.com/search.json?  result_type=popular&q=%E3%81%8A%E3%81%AF %E3%82%88%E3%81%86
  • 13. Web API •  LWP::UserAgent –  Others: LWP::Simple, URI::Fetch •  HTTP::Request •  –  GET/POST/PUT/DELETE/HEAD •  HTTP::Response
  • 14. LWP::UserAgent use URI;  use LWP::UserAgent;  my $uri = URI‐>new('http://search.twitter.com/ search.json?q=perl');  my $ua  = LWP::UserAgent‐>new();  my $res = $ua‐>get( $uri );  die $res‐>status_line if $res‐>is_error;  print $res‐>content . "n"; 
  • 15. •  cpanm JSON -> JSON::XS or JSON::PP •  JSON –  uXXXX Unicode ascii –  –  u304au306fu3088u3046
  • 16. JSON use JSON qw( decode_json );  use Encode qw( encode_utf8 );  my $json_text =   '{"user":"yusukebe","message":"u304au306fu3088u3046"}';  my $ref = decode_json($json_text);  print encode_utf8("$ref‐>{user} : $ref‐>{message}n");  yusukebe :   
  • 17. •  URI URI •  LWP::UserAgent URI / •  JSON
  • 18. •  #yokohamapm use LWP::UserAgent;  use URI;  use JSON qw(decode_json);  use Encode qw(encode_utf8);  my $query  = '#yokohamapm';  my $uri  = URI‐>new('http://search.twitter.com/search.json'); 
  • 19. my $params = {      q           => $query,      result_type => 'recent',  };  $uri‐>query_form(%$params);  my $ua   = LWP::UserAgent‐>new;  my $res  = $ua‐>get($uri);  die "Error: ". $res‐>status_line if $res‐>is_error;  my $json = $res‐>content;  my $ref  = decode_json($json);  for my $tweet ( @{ $ref‐>{results} } ) {  print encode_utf8("@$tweet‐>{from_user}: $tweet‐>{text}n");  } 
  • 20. $ perl twitter_search.pl  @mazkeng:  ust  #yokohamapm  @hokke_mirin:  2 #perlcasual  #yokohamapm  @yusukebe:   #yokohamapm 
  • 21. WebService::Simple use WebService::Simple;  use Data::Dumper;  my $query  = '#yokohamapm';  my $service = WebService::Simple‐>new(      base_url  => 'http://search.twitter.com/search.json',      response_parser => 'JSON',  );  my $res  = $service‐>get( { q => $query });  my $ref = $res‐>parse_response();  print Dumper $ref; 
  • 22. •  Web API = •  Standing on the shoulders of giants - -
  • 23. ? WEB+DB PRESS vol.58 2010 8 Perl Hackers Hub