Your SlideShare is downloading. ×
0
Perl Web API                     

Web API Perl                     
          yusukebe
          2010/07/23
 Yokohama.pm#...
•  Perl                      
•  Web API                   &&Perl              
•          /                              ...
Web API            
•  API = Application Programming Interface
  –          /
             
•  Web API
                   ...
Web API   
•    Twitter API
•    Google Ajax API
•    YouTube API
•    Flickr API
•    mixi        API
•             API
•...
/   
•  Web                        
  –  HTTP
  –  URI
  –  GET/POST
  –  JSON/XML etc.
3                 (GET   )
1.        URI   
2.  GET                
3. 
•  Twitter       perl                                
  –  http://search.twitter.com/search.json?q=perl
•  GET            ...
Twitter Search API
•  Twitter                                  
•  http://dev.twitter.com/doc/get/search
•                ...
API                                             
•                 URI
     –  http://search.twitter.com/search.json
•    ...
Web
URI                 
•             ASCII                    
•  URI            
•  http://search.twitter.com/search.json?q...
URI                    
use URI; 
use utf8; 
my $query  = '       '; 
my $uri    = 
URI‐>new('http://search.twitter.com/se...
Web API                                  
•  LWP::UserAgent          
  –  Others: LWP::Simple, URI::Fetch
•  HTTP::Reques...
LWP::UserAgent                               

use URI; 
use LWP::UserAgent; 

my $uri = URI‐>new('http://search.twitter.c...
•  cpanm JSON -> JSON::XS or JSON::PP
•  JSON             
  –  uXXXX       Unicode        ascii
             
  –        ...
JSON                        
use JSON qw( decode_json ); 
use Encode qw( encode_utf8 ); 

my $json_text =  
'{"user":"yusu...
•  URI                  URI     
•  LWP::UserAgent         URI
       /            
•  JSON
•     #yokohamapm
                                                                 

use LWP::UserAgent; 
use URI; 
use JS...
my $params = { 
    q           => $query, 
    result_type => 'recent', 
}; 

$uri‐>query_form(%$params); 
my $ua   = LWP...
$ perl twitter_search.pl 
@mazkeng:      ust                    #yokohamapm 
@hokke_mirin:           2            #perlcas...
WebService::Simple
use WebService::Simple; 
use Data::Dumper; 

my $query  = '#yokohamapm'; 
my $service = WebService::Sim...
•  Web API =
               
•    Standing on the shoulders of giants -


               -
?
WEB+DB PRESS vol.58
 2010 8             
  Perl Hackers Hub
Upcoming SlideShare
Loading in...5
×

PerlでWeb API入門

4,957

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,957
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
28
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "PerlでWeb API入門"

  1. 1. Perl Web API 
 Web API Perl yusukebe 2010/07/23 Yokohama.pm#6 x PerlCasual#3
  2. 2. •  Perl •  Web API &&Perl •  / –  LWP::* •  –  20 : Web API ← –  30 : sugyan •  ( )
  3. 3. Web API •  API = Application Programming Interface –  / •  Web API API
  4. 4. Web API •  Twitter API •  Google Ajax API •  YouTube API •  Flickr API •  mixi API •  API •  etc…
  5. 5. / •  Web –  HTTP –  URI –  GET/POST –  JSON/XML etc.
  6. 6. 3 (GET ) 1.  URI 2.  GET 3. 
  7. 7. •  Twitter perl –  http://search.twitter.com/search.json?q=perl •  GET •  JSON • 
  8. 8. Twitter Search API •  Twitter •  http://dev.twitter.com/doc/get/search • 
  9. 9. API •  URI –  http://search.twitter.com/search.json •  –  JSON/atom •  –  GET •  –  q=perl
  10. 10. Web
  11. 11. URI •  ASCII •  URI •  http://search.twitter.com/search.json?q= %E3%81%8A%E3%81%AF %E3%82%88%E3%81%86
  12. 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. 13. Web API •  LWP::UserAgent –  Others: LWP::Simple, URI::Fetch •  HTTP::Request •  –  GET/POST/PUT/DELETE/HEAD •  HTTP::Response
  14. 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. 15. •  cpanm JSON -> JSON::XS or JSON::PP •  JSON –  uXXXX Unicode ascii –  –  u304au306fu3088u3046
  16. 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. 17. •  URI URI •  LWP::UserAgent URI / •  JSON
  18. 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. 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. 20. $ perl twitter_search.pl  @mazkeng:  ust  #yokohamapm  @hokke_mirin:  2 #perlcasual  #yokohamapm  @yusukebe:   #yokohamapm 
  21. 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. 22. •  Web API = •  Standing on the shoulders of giants - -
  23. 23. ? WEB+DB PRESS vol.58 2010 8 Perl Hackers Hub
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×