雅虎天气API使用笔记

查询时base_url:
https://query.yahooapis.com/v1/public/yql?q= , 公共查询接口,有限制查询次数
https://query.yahooapis.com/v1/yql?q= , 注册后获取ApkId,有限制查询次数但是已经足够使用了,10万次/天


第一步,手机端定位用户的位置(根据IP查询,或者。。。。),得到用户所在城市名称,或者城市名称的拼音,或者经纬度

第二步,根据用户所在城市的中文、英文、经纬度,获取woeid:


1,这里text键值可以用 中文、英文、经纬度。如


1): select * from geo.placefinder where text="Guangzhou"


2): select * from geo.placefinder where text="北京"


3): select * from geo.placefinder where text="39.9919336,116.3404132"  and gflags = "R"


2,使用如下url查询城市的woeid:三种方法均可使用
https://query.yahooapis.com/v1/public/yql?q=select * from geo.placefinder where text="39.9919336,116.3404132"  and gflags = "R"&format=json
https://query.yahooapis.com/v1/public/yql?q=select * from geo.placefinder where text="北京"&format=json
https://query.yahooapis.com/v1/public/yql?q=select * from geo.placefinder where text="Guangzhou"&format=json


注意使用经纬度获取woeid时一直取不到,后来才知道时缺少一个参数 fglags参数。,必须加 and gflags = "R"这一串才行


返回的xml格式的数据:



40
23.12911
113.26336
23.12911
113.26336
17700


Guangzhou
Guangdong
People's Republic of China







Guangzhou

Guangdong
People's Republic of China
CN
44


2161838 ,这个就是需要的woeid
7










第三步,根据上面返回的结果中得到woeid,查询天气数据:
1):把第一步获取的woeid放到下面的查询语句中:
select * from weather.forecast where woeid=2502265 and u="c"


2):最后使用如下url即可得到天气数据:
https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid=2502265 and u="c"&format=json


返回的xml格式的数据:





Yahoo! Weather - Sunnyvale, CA

http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_c.html

Yahoo! Weather for Sunnyvale, CA
en-us
Wed, 27 May 2015 7:55 pm PDT
60






Yahoo! Weather
142
18
http://weather.yahoo.com

http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif



Conditions for Sunnyvale, CA at 7:55 pm PDT
37.37
-122.04

http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_c.html

Wed, 27 May 2015 7:55 pm PDT



Current Conditions:
Fair, 15 C

Forecast:
Wed - Partly Cloudy. High: 21 Low: 12
Thu - AM Clouds/PM Sun. High: 23 Low: 12
Fri - AM Clouds/PM Sun. High: 24 Low: 13
Sat - AM Clouds/PM Sun. High: 24 Low: 13
Sun - AM Clouds/PM Sun. High: 22 Low: 14

Full Forecast at Yahoo! Weather

(provided by The Weather Channel)

]]>






USCA1116_2015_05_31_7_00_PDT









注:
url后面的参数为设置返回数据类型,json或xml


这样就能拿到以摄氏度返回的天气了。
这里注意一点,天气的返回值里面有一个code,那个代表的是各种天气类型,暴风雨啊龙卷风啊冰雹啊之类的。






code与其对应天气状态:


0 tornado
1 tropical storm
2 hurricane
3 severe thunderstorms
4 thunderstorms
5 mixed rain and snow
6 mixed rain and sleet
7 mixed snow and sleet
8 freezing drizzle
9 drizzle
10 freezing rain
11 showers
12 showers
13 snow flurries
14 light snow showers
15 blowing snow
16 snow
17 hail
18 sleet
19 dust
20 foggy
21 haze
22 smoky
23 blustery
24 windy
25 cold
26 cloudy
27 mostly cloudy (night)
28 mostly cloudy (day)
29 partly cloudy (night)
30 partly cloudy (day)
31 clear (night)
32 sunny
33 fair (night)
34 fair (day)
35 mixed rain and hail
36 hot
37 isolated thunderstorms
38 scattered thunderstorms
39 scattered thunderstorms
40 scattered showers
41 heavy snow
42 scattered snow showers
43 heavy snow
44 partly cloudy
45 thundershowers
46 snow showers
47 isolated thundershowers
3200 not available

你可能感兴趣的:(API,PHP)