天天看點

請求Google的服務

# Sample code from Programing Ruby, page 239

require 'soap/wsdlDriver'

以下通過對Google的服務請求,使用Google的一個服務,并使用了WSDL(Web Services Description Language)來完成.(前提是你擁有一個Google Web API的使用賬号,并儲存在主目錄的.goolge_key檔案中)

require 'cgi'

WSDL_URL = "http://api.google.com/GoogleSearch.wsdl"

soap = SOAP::WSDLDriverFactory.new(WSDL_URL).createDriver

query = 'pragmatic'

key = File.read(File.join(ENV['HOME'], ".google_key")).chomp

result = soap.doGoogleSearch(key, query, 0, 1, false,

                             nil, false, nil, nil, nil)

printf "Estimated number of results is %d.\n",

       result.estimatedTotalResultsCount

printf "Your query took %6f seconds.\n", result.searchTime

first = result.resultElements[0]

puts first.title

puts first.URL

puts CGI.unescapeHTML(first.snippet)

繼續閱讀