#!/usr/bin/perl -w use strict; use CGI::Fast; use LWP::Simple; use XML::Simple; use JSON; # Yahoo! JAPAN Web Search API my $YJWS_APPID = "[insert your AppID]"; my $YJWS_API_URL = 'http://api.search.yahoo.co.jp/WebSearchService/V1/webSearch'; # Pre-construct objects my $parser = XML::Simple->new(); #my $json = JSON->new(pretty => 1, delimiter => 0); my $json = JSON->new(); while (my $q = new CGI::Fast) { my $query = $q->param('query'); if (!$query) { show_form(); next; } # construct URL for Yahoo! API my $url = "$YJWS_API_URL?appid=$YJWS_APPID&query=$query"; for my $param (qw/type results start format adult_ok similar_ok language country site/) { $url .= '&' . $param . '=' . $q->param($param) if defined($q->param($param)) && $q->param($param); } # fetch and parse my $res = get($url); if ($res =~ /XMLin($res); # drop garbage for my $col (qw/xmlns xmlns:xsi xsi:schemaLocation/) { delete $data->{$col}; } if ($data->{totalResultsReturned} == 1) { $data->{Result} = [ $data->{Result} ]; } for my $result (@{$data->{Result}}) { for my $col (qw/MimeType ModificationDate Cache ClickUrl/) { delete $result->{$col}; } } # render to JSON print "Content-Type: text/plain\n\n"; print $json->objToJson($data); } sub show_form { print << "EOD"; Content-Type: text/html Yahoo! JAPAN Web Search (JSON)

Yahoo! JAPAN Web Search (JSON)

query:
type: (default: all)
all
any
phrase
results: (default: 10, max: 50)
start: (default: 1)
format: (default: any)
any
html
msword
pdf
ppt
rss
txt
xls
adult_ok: (default: none)
similar_ok: (default: none)
language: (default: ja)
(supported languages)
country: (default: none)
(supported countries)
site: (default: none)

SEE ALSO: http://developer.yahoo.co.jp/search/web/V1/webSearch.html

EOD }