Dec 16, 2005
Google Web APIs (beta) にチャレンジ
Google Web APIs (beta) とは
Google が公開している Webサービス用の API。 Google に SOAP でリクエストを送ると、SOAP レスポンスで処理結果が帰ってくる。 対応しているサービスは、キャッシュの取得、検索、スペルチェック。
- Google Web APIs (beta)
- http://www.google.com/apis/
- Google Web APIs (beta) - Reference
- http://www.google.com/apis/reference.html
- License
- http://www.google.com/apis/api_terms.html
試してみる
試してみた。 Google Web APIs の呼び出し方は色々あるが、とりあえず今回は直接 POST でリクエストを送り込むことにする。 POST の送信に使用した HTML は↓に添付する。
- 最初に Google Web APIs license key の取得 を行う。
- リクエスト用の SOAP メッセージを作成する。メッセージには 1 で取得したライセンスキーを埋め込む必要がある。Google Web APIs Developer's Kit をダウンロードするとサンプルの SOAP メッセージが入っているので、それを参考にするとラク。
- 早速リクエストを送ってみる。"http://api.google.com/search/beta2" に対して 2 で作成した SOAP メッセージを送信する。
Google Web APIs 呼び出し用 HTML
POST の送信には JavaScript を利用して ajax っぽく(笑)してみることに。
<html>
<head>
<script>
<!--
var req;
function sendRequest() {
var url = document.myform.url.value;
var param = document.myform.request.value;
req = prepareXMLHttpRequest();
if (req == null) {
alert('XMLHttpRequest is not supported');
return;
}
req.onreadystatechange = showResponse;
req.open("POST", url, true);
req.setRequestHeader("Content-Type" , "text/xml");
req.send(param);
}
function showResponse() {
if (req.readyState == 4) {
if (req.status == 200) {
document.myform.response.value = req.responseText;
} else {
document.myform.response.value = 'FAILURE!! ' + req.status + ':' + req.statusText;
}
}
}
function prepareXMLHttpRequest() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
//-->
</script>
</head>
<body>
<form name="myform">
<input type="button" value="POST!!" onclick="sendRequest();"><br>
<br>
URL:<br>
<input type="text" name="url" size="100"><br>
<br>
REQUEST:<br>
<textarea name="request" cols="100" rows="25"></textarea><br>
<br>
RESPONSE:<br>
<textarea name="response" cols="100" rows="25"></textarea>
</form>
</body>
</html>
検索用 SOAP リクエスト(サンプル)
<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Body>
<ns1:doGoogleSearch xmlns:ns1="urn:GoogleSearch"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<key xsi:type="xsd:string">[Google Web APIs license key]</key>
<q xsi:type="xsd:string">Google Web APIs</q>
<start xsi:type="xsd:int">0</start>
<maxResults xsi:type="xsd:int">10</maxResults>
<filter xsi:type="xsd:boolean">true</filter>
<restrict xsi:type="xsd:string"></restrict>
<safeSearch xsi:type="xsd:boolean">false</safeSearch>
<lr xsi:type="xsd:string"></lr>
<ie xsi:type="xsd:string">latin1</ie>
<oe xsi:type="xsd:string">latin1</oe>
</ns1:doGoogleSearch>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
TrackBack ping me at
http://www.in-vitro.jp/blog/index.cgi/WebService/20051216_01.trackback
writeback message: Ready to post a comment.
