适应手机端的天气预报页面
shenzhen.pnghttp://m.weather.com.cn
奇怪的是 在微信浏览器里面 必须要带上经纬度http://m.weather.com.cn/d/town/index?lat=$lat&lon=$lon
更多关于天气的接口
https://blog.csdn.net/x_iya/article/details/52189750
https://blog.csdn.net/weixin_41830601/article/details/81661611
原文 链接 https://zhidao.baidu.com/question/1989073542429544307.html
http://flash.weather.com.cn/wmaps/xml/china.xml
http://flash.weather.com.cn/wmaps/xml/hubei.xml
感觉也不是所有的省份名都行 广东的就没有
http://flash.weather.com.cn/wmaps/xml/城市拼音.xml
根据 拼音来的
得到的数据是xml格式的
数据有两种结果
http://flash.weather.com.cn/wmaps/xml/shenzhen.xml
http://flash.weather.com.cn/wmaps/xml/weihai.xml
weihai.png
处理方式: (很草率啊.. 赶得紧,就先这样。)$pinyin = new \Pin\Pinyin\pinyin(); $result = $pinyin->pinyin(substr(I('post.city'), 0, -1)); // 将接收的城市名称转换为拼音 $url = 'http://flash.weather.com.cn/wmaps/xml/'.$result.'.xml'; $xml = $this->curl_get($url); // 获取xml $xml = (array)simplexml_load_string($xml,'SimpleXMLElement', LIBXML_NOCDATA); $jsonArray = json_decode(json_encode($xml),true); // 将xml 解析为普通数组 // 如果接口返回的值有两个以上 if ($jsonArray['city'][0] == null) { $list['stateDetailed'] = $jsonArray['city']['@attributes']['stateDetailed']; $list['windPower'] = $jsonArray['city']['@attributes']['windPower']; } else { $list['stateDetailed'] = $jsonArray['city'][0]['@attributes']['stateDetailed']; $list['windPower'] = $jsonArray['city'][0]['@attributes']['windPower']; } /** * curl get method */ function curl_get($url){ if(function_exists('file_get_contents')){ $file_contents = file_get_contents($url); } else{ $ch = curl_init(); $timeout = 5; curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $file_contents = curl_exec($ch); curl_close($ch); } return $file_contents; }
网友评论