---
title: "在Rails程序里面提供“天气预报”信息"
author: "gm8pleasure"
date: "2007-08-22T05:14:32Z"
tags: [".net", "Rails", "WebService"]
slug: "rails"
draft: false
lastmod: "2007-08-22T05:14:32Z"
id: 115399
url: "https://pleasure.iteye.com/blog/115399"
---

在Rails程序里面提供天气预报，我的做法是通过调用WebService来获得天气预报信息。在这个[网站](http://www.webxml.com.cn/)里面提供了一个WebService，网站数据是从中国气象局得来的，应该说是比较权威。这个WebService从接口方式来看，应该是用.Net编写的。

ASMX文件地址：[http://www.webxml.com.cn/WebServices/WeatherWebService.asmx](http://www.webxml.com.cn/WebServices/WeatherWebService.asmx "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx")

在Rails可以用soap4r来调用这个.net WebService

第一步骤，安装soap4r

gem install soap4r

第二步骤，调用soap4r来自动生产WebService的Wrap类

`wsdl2ruby.rb --wsdl http://www.webxml.com.cn/WebServices/WeatherWebService.asmx --type client --force`

`第三部，在Rails里面调用（我的一个调用过程）`

`在Controller里面`

`def index`

> `require 'WeatherAPIDriver.rb'`
> 
> weatherService=WeatherWebServiceSoap.new  
> city=GetWeatherbyCityName.new("54830") #54830=zibo  
> cityweather=weatherService.getWeatherbyCityName(city)
> 
> 这样就可以了！
> 
> del.icio.us 标记: [rails](http://del.icio.us/popular/rails), [weather](http://del.icio.us/popular/weather), [soap4r](http://del.icio.us/popular/soap4r)

![](http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=-1842374001444208629&page=RSS%3A%20%E5%9C%A8Rails%E7%A8%8B%E5%BA%8F%E9%87%8C%E9%9D%A2%E6%8F%90%E4%BE%9B%E2%80%9C%E5%A4%A9%E6%B0%94%E9%A2%84%E6%8A%A5%E2%80%9D%E4%BF%A1%E6%81%AF&referrer=)![](http://c.live.com/c.gif?NC=31263&amp%3BNA=1149&amp%3BPI=88469&amp%3BRF=&amp%3BDI=3919&amp%3BPS=85545&amp%3BTP=pleasure8.spaces.live.com&amp%3BGT1=pleasure8)