雅虎天气api调用 记录
有个小app程序需要获取指定城市的天气,国内或者国外,由于google weather api停用 国内中央气象台的拿不到国外的。
先用雅虎的进行读取,但是还无法保证准确度。参照已有的资料进行了一下测试。
大概分为 获取城市代码,根据城市代码获取天气这里以php代码为例子
下面接口用于返回城市大天气代码 根据command参数返回近似的城市名称,例如输入北京会返回 北京相关的城市,如果输入北京市则只会返回
china表示的北京市,
http://sugg.us.search.yahoo.net/gossip-gl-location/?appid=weather&output=xml&command=北京
获取天气接口
u中的则就是天气代码
$url = 'http://weather.yahooapis.com/forecastrss?w='.$city_code.'&u=23424781';
那么接下来读取天气相关信息
$yweather = "http://xml.weather.yahoo.com/ns/rss/1.0"; //命名空间
$res = new DOMDocument();
$res->load($url);
$node = $res->getElementsByTagNameNS($yweather, 'atmosphere');
$humi = $node->item(0)->attributes->item(0)->nodeValue; //获取湿度
$node = $res->getElementsByTagNameNS($yweather, 'condition');
$code = $node->item(0)->attributes->item(1)->nodeValue; //获取天气代码
$temp = $node->item(0)->attributes->item(2)->nodeValue; //获取温度
$a_result=array('humi'=>$humi,'code'=>$code,'temp'=>$temp);
参考的一片比较简单明了的博客 地址为http://www.oschina.net/code/snippet_876708_20565。
要突出显示,尊重分享者的劳动成果嘎嘎。

浙公网安备 33010602011771号