Currently, the weather forecast function is often available on the home pages of various websites. How do they achieve this? In fact, this is quite simple. Now the meteorological station, Baidu, Google, youdao, and so on provide us with good service support. We can call it directly.
Currently, there are two main ways to provide services for websites:
1. Plug-ins
This method is relatively simple. You can search for "Free Weather Forecast" on the Internet to find the free weather forecast plug-in provided by the relevant website and directly copy the automatically generated code. The disadvantage is that it is not very flexible. It is only limited to the several styles provided by the website. You cannot customize styles by yourself, and most of them are in the IFRAME format, which cannot be well controlled by yourself, and some advertisements may not be avoided. Here I will only provide a few links:
Weather Network: http://service.weather.com.cn/plugin/index.shtml
Thinkpage: http://www.thinkpage.cn/weather/widget.aspx
WEATHER: http://t.xidie.com/WeatherCode.aspx
Weather 121: http://weather.xixik.com/
WEATHER: http://www.tianqi8.com/code/
Free weather query: http://www.fc315.net/11/code/
636600: http://www.636600.com/code/index.html
2. API request address Interface
This method is very flexible and can be controlled by developers, but the disadvantage is that it is difficult and the program must maintain the information related to the weather forecast. Here are a few examples to illustrate.
Google weather forecast API address: http://www.google.com/ig/api? Weather = Beijing
After entering the information, you can view the weather forecast information of Beijing. It is displayed in XML format and we only need to extract the required information from it.
Sina weather forecast API address: http://php.weather.sina.com.cn/xml.php? City = Beijing & Password = djoyniet8234jlsk & day = 0
City stands for the city, and "Wuhan" is directly written, but "Wuhan" cannot be used; password is fixed; Day is 0 indicates the weather of the Day, 1 indicates the next day, 2 indicates the third day, and so on. The maximum value is 4.
It returns the same XML format, which is divided into two parts, representing "today's day" and "Today's night ". For the meaning of each label in XML, refer to Sina weather forecast Network: http://weather.news.sina.com.cn/
Weather Network: http://m.weather.com.cn/data/101010100.html
Among them, 101010100 is the city code, and your city id is dynamically queried by http: // 61.4.185.48: 81/G/Based on your local IP address, return the city weather information based on the city id. City Id see http://vyphn.iteye.com/blog/850431
It returns city weather information in JSON format, making it easy to process.
Thinkpage: http://www.thinkpage.cn/weather/api.svc/getWeather? City = 101010100 & language = ZH-CHS & provider = CMA & unit = C & Key =
Thinkpage documentation is relatively complete, see the http://www.thinkpage.cn/weather/api/ for details
It also returns information in JSON format.
For APIs returned in XML format, the basic idea is to use the address to load the XML file to the file stream or download it, And then parse it.
For details about how to parse JSON, see
Http://www.json.org/json-zh.html
Well, I will talk about it here. It is not difficult to find a way to implement it, so I did not give a specific example.