Google Web APIs (beta) の利用


  Google Web APIs License Key の取得

http://www.google.com/apis/index.html よりアカウントを作成して取得して下さい。
メールが送られて来て、Your Google Web APIs license key is の後にあるのがキーです。

  Perl コード




入力は Shift_JIS で、出力が UTF-8 を想定しています。
( ソースコードは utf-8n で保存して下さい )

引数は、query が検索文字列で、start が開始位置です。

※ 同じサイトが二つならんでいる場合は、二つ目の位置を start で指定できません。
※ 次のページは start=10 というふうに指定します。

( 1回のクエリーでは最高10件までです。1日に1000クエリーまでです )
http://www.google.co.jp/ からの検索と全く同じではありません )

GoogleSearch.wsdl は Google Web APIs Developer's Kit に入っています。
( http://www.google.com/apis/download.html よりダウンロード )

  

#!/usr/local/bin/perl

$mykey = 'ライセンスキー';

use Encode;
use SOAP::Lite;

print "Expires: Thu, 04 Oct 2000 00:00:00 GMT\n";
print "Content-Type: text/html; Charset=utf-8\n";
print "\n";

binmode(STDOUT, ":utf8");

$METHOD = $ENV{'REQUEST_METHOD'};
$METHOD =~ tr/a-z/A-Z/;
$QUERY_STRING = $ENV{'QUERY_STRING'};

if ( $METHOD eq "POST" ) {
	read(STDIN, $Form, $ENV{'CONTENT_LENGTH'});
	@Fields_Data = split(/&/, $Form);
}

@Fields_Data2 = split(/&/, $QUERY_STRING);

foreach $Field_Data ( @Fields_Data ) {
	($Name, $Value) = split(/=/, $Field_Data);
	$Value =~ tr/+/ /;
	$Value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	$_POST{$Name} = $Value;
}
foreach $Field_Data2 ( @Fields_Data2 ) {
	($Name, $Value) = split(/=/, $Field_Data2);
	$Value =~ tr/+/ /;
	$Value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	$_POST{$Name} = $Value;
	$_GET{$Name} = $Value;
}

$query = $_POST{'query'};
Encode::from_to($query, "shift_jis", "utf8");
utf8::decode($query);
if ( $_POST{'start'} eq '' ) {
	$start = '0';
}
else {
	$start = $_POST{'start'};
}
utf8::decode($start);

@arguments = (
   $mykey,
   $query,
   $start,
   '10',
   'false',
   '',
   'false',
   'lang_ja',
   'UTF-8',
   'UTF-8'
);



$googleSearch = SOAP::Lite->service("file:GoogleSearch.wsdl");
#$googleSearch = SOAP::Lite->service("http://api.google.com/GoogleSearch.wsdl");

$result = 
	$googleSearch
		->doGoogleSearch(@arguments);

print "<pre>";

print $result->{'estimatedTotalResultsCount'} . "\n";

for ( $i = 0; $ i < $result->{'endIndex'}; $i++ ) {
	print $result->{'resultElements'}[$i]->{'URL'} . "\n";
	print $result->{'resultElements'}[$i]->{'title'} . "\n";
	print $result->{'resultElements'}[$i]->{'snippet'} . "\n";
	print "\n";
}

print "</pre>";
  

  結果の構造の取得

結果の構造は PHP で以下のコードを実行すると良いと思います。
PHP で作成してしまえば良いのですが、現状ではいろいろ環境に問題があります。

以下は php4 用のコードです。

nusoap.php は ※ より取得して下さい
( 5895 行の // Parse the XML file. の次に $xml = utf8_encode($xml); を追加して下さい )
※ 使用したバージョン↓
$Id: nusoap.php,v 1.95 2006/02/02 15:52:34 snichol Exp $

ソースコードは utf-8n で保存して下さい
( ※ 2009/02/24 現在 => http://sourceforge.net/projects/nusoap/ )
この記事は 2006/02/19 に作成されているので上記場所のソフトと内容と合致していない可能性があります。
この修正はリンク切れの対応の為に行っています

  

<?
header( "Content-Type: text/html; Charset=utf-8" );

require_once('nusoap.php');

mb_language( 'ja' );
mb_internal_encoding('UTF-8');

$parameters = array(
  'key'=>'ライセンスキー',
  'q' => 'PHP',
  'start' => '0',
  'maxResults' => '10',
  'filter' => 'false',
  'restrict' => '',
  'safeSearch' => 'false',
  'lr' => 'lang_ja',
  'ie' => 'UTF-8',
  'oe' => 'UTF-8'
);


$client = new soapclient('http://api.google.com/GoogleSearch.wsdl','wsdl');

$result = $client->call('doGoogleSearch',$parameters);
?>

<HTML>
<HEAD>
<TITLE>Google API テスト</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
</HEAD>
<BODY>
<PRE>
<?
var_dump(
	$result
);
?>
</PRE>
</BODY>
</HTML>
  

  

array
  'directoryCategories' => 
    array
      0 => 
        array
          'fullViewableName' => 'Top/Computers/Programming/Languages/PHP'
          'specialEncoding' => ''
  'documentFiltering' => false
  'endIndex' => 10
  'estimateIsExact' => false
  'estimatedTotalResultsCount' => 1870000
  'resultElements' => 
    array
      0 => 
        array
          'URL' => 'http://www.php.gr.jp/'
          'cachedSize' => '12k'
          'directoryCategory' => 
            array
              'fullViewableName' => ''
              'specialEncoding' => ''
          'directoryTitle' => ''
          'hostName' => ''
          'relatedInformationPresent' => true
          'snippet' => '日本<b>PHP</b>ユーザ会 TOP PAGE. '
          'summary' => ''
          'title' => '日本<b>PHP</b>ユーザ会'

この間に1〜9の表示

  'searchComments' => ''
  'searchQuery' => 'PHP'
  'searchTime' => 0.043311
  'searchTips' => ''
  'startIndex' => 1
  




yahoo  google  MSDN  MSDN(us)  WinFAQ  Win Howto  tohoho  ie_DHTML  vector  wdic  辞書  天気 


[smalltech]
claudebot
24/03/29 17:03:09
InfoBoard Version 1.00 : Language=Perl

1 BatchHelper COMprog CommonSpec Cprog CprogBase CprogSAMPLE CprogSTD CprogSTD2 CprogWinsock Cygwin GameScript HTML HTMLcss InstallShield InstallShieldFunc JScript JScriptSAMPLE Jsfuncs LLINK OldProg OracleGold OracleSilver PRO PRObrowser PROc PROconePOINT PROcontrol PROftpclient PROjscript PROmailer PROperl PROperlCHAT PROphp PROphpLesson PROphpLesson2 PROphpLesson3 PROphpfunction PROphpfunctionArray PROphpfunctionMisc PROphpfunctionString PROsql PROvb PROvbFunction PROvbString PROvbdbmtn PROvbonepoint PROwebapp PROwin1POINT PROwinSYSTEM PROwinYOROZU PROwindows ProjectBoard RealPHP ScriptAPP ScriptMaster VBRealtime Vsfuncs a1root access accreq adsi ajax amazon argus asp aspSample aspVarious aspdotnet aw2kinst cappvariety centura ckeyword classStyle cmaterial cmbin cmdbapp cmenum cmlang cmlistbox cmstd cmstdseed cmtxt cs daz3d db dbCommon dbaccess dnettool dos download flex2 flex3 flex4 framemtn framereq freeWorld freesoft gimp ginpro giodownload google hdml home hta htmlDom ie9svg install java javaSwing javascript jetsql jquery jsp jspTest jspVarious lightbox listasp listmsapi listmsie listmsiis listmsnt listmspatch listmsscript listmsvb listmsvc memo ms msde mysql netbeans oraPlsql oracle oracleWiper oraclehelper orafunc other panoramio pear perl personal pgdojo pgdojo_cal pgdojo_holiday pgdojo_idx pgdojo_ref pgdojo_req php phpVarious phpguide plsql postgres ps r205 realC realwebapp regex rgaki ruby rule sboard sc scprint scquest sdb sdbquest seesaa setup sh_Imagick sh_canvas sh_dotnet sh_google sh_tool sh_web shadowbox shgm shjquery shvbs shweb sjscript skadai skywalker smalltech sperl sqlq src systemdoc tcpip tegaki three toolbox twitter typeface usb useXML vb vbdb vbsfunc vbsguide vbsrc vpc wcsignup webanymind webappgen webclass webparts webtool webwsh win8 winofsql wmi work wp youtube