这个无需PHP做。前端就可以获取,直接搜索天气API,会有很多天气接口。
或者
最近在做微信公众平台测试时,想在里面子菜单上添加查询未来几天(包括今天)天气的功能,就查找了下好用的天气预报查询接口API,使用比较多的有:国家气象局天气接口、新浪天气预报接口、百度天气预报接口、google天气接口、Yahoo天气接口等等,我使用的是百度提供的免费天气查询接口API,下面与大家分享下...
1、查询方式:
百度提供的是根据纬度和城市名查询天气情况
2、接口事例:
3、接口参数说明:
4、返回结果说明:
5、
//城市名
$city = '上海';
//对json格式的字符串进行编码
$arr =json_decode($str,TRUE);
print_r($atr);
//城市名
$city = '上海';
//获取json格式的数据
$str =file_get_contents("".$city."output=jsonak=5slgyqGDENN7Sy7pw29IUvrZ");
//对json格式的字符串进行编码
$arr =json_decode($str,TRUE);
print_r($atr);
6、返回页面的是json编码后的数据:
[plain] view plain copy print?
meta charset="UTF-8"
Array
(
[error] = 0
[status] = success
[date] = 2014-03-17
[results] = Array
(
[0] = Array
(
[currentCity]= 上海
[weather_data]= Array
(
[0]= Array
(
[date] = 周一(今天, 实时:19℃)
[dayPictureUrl] =
[nightPictureUrl] =
[weather] = 晴
[wind] = 西南风3-4级
[temperature] = 13℃
)
[1] = Array
(
[date]= 周二
[dayPictureUrl] =
[nightPictureUrl] =
[weather]= 多云转阴
[wind]= 东北风3-4级
[temperature] = 24 ~ 9℃
)
[2] = Array
(
[date]= 周三
[dayPictureUrl] =
[nightPictureUrl] =
[weather]= 中雨转小雨
[wind]= 东北风3-4级
[temperature] = 15 ~ 8℃
)
[3] = Array
(
[date]= 周四
[dayPictureUrl] =
[nightPictureUrl] =
[weather]= 多云转晴
[wind]= 北风3-4级
[temperature] = 14 ~ 6℃
)
)
)
)
)
meta charset="UTF-8"
Array
(
[error] = 0
[status] = success
[date] = 2014-03-17
[results] = Array
(
[0] = Array
(
[currentCity]= 上海
[weather_data]= Array
(
[0]= Array
(
[date] = 周一(今天, 实时:19℃)
[dayPictureUrl] =
[nightPictureUrl] =
[weather] = 晴
[wind] = 西南风3-4级
[temperature] = 13℃
)
[1] = Array
(
[date]= 周二
[dayPictureUrl] =
[nightPictureUrl] =
[weather]= 多云转阴
[wind]= 东北风3-4级
[temperature] = 24 ~ 9℃
)
[2] = Array
(
[date]= 周三
[dayPictureUrl] =
[nightPictureUrl] =
[weather]= 中雨转小雨
[wind]= 东北风3-4级
[temperature] = 15 ~ 8℃
)
[3] = Array
(
[date]= 周四
[dayPictureUrl] =
[nightPictureUrl] =
[weather]= 多云转晴
[wind]= 北风3-4级
[temperature] = 14 ~ 6℃
)
)
)
)
)
7、PHP中自带了处理json格式字符串的内置函数,下面做一个事例,并给出完整代码:
[php] view plain copy print?
metacharset="UTF-8"
?php
//城市名
$city = '上海';
//获取json格式的数据
$str = file_get_contents("".$city."output=jsonak=5slgyqGDENN7Sy7pw29IUvrZ");
//对json格式的字符串进行编码
$arr = json_decode($str,TRUE);
echo "城市:".$arr['results'][0]['currentCity']." 日期:".$arr['date']."br /br /";
foreach($arr['results'][0]['weather_data']as $val)
{
echo $val['date']."br/";
echo "天气:{$val['weather']}br/";
echo "风向:{$val['wind']}br/";
echo "温度:{$val['temperature']}br/br /";
}
?
metacharset="UTF-8"
?php
//城市名
$city = '上海';
//获取json格式的数据
$str = file_get_contents("".$city."output=jsonak=5slgyqGDENN7Sy7pw29IUvrZ");
//对json格式的字符串进行编码
$arr = json_decode($str,TRUE);
echo "城市:".$arr['results'][0]['currentCity']." 日期:".$arr['date']."br /br /";
foreach($arr['results'][0]['weather_data']as $val)
{
echo $val['date']."br/";
echo "天气:{$val['weather']}br/";
echo "风向:{$val['wind']}br/";
echo "温度:{$val['temperature']}br/br /";
}
?
8、返回的内容如下:
?php
$URLStyle = "";
$chinaURL = sprintf($URLStyle, "china");
$chinaStr = file_get_contents($chinaURL);
$chinaObj = simplexml_load_string($chinaStr);
$chinaObjLen = count($chinaObj-city);
echo "chinaObjLen = ".$chinaObjLen."\n";
for ($i=0;$i$chinaObjLen;$i++){
//遍历省一级节点,共37个
$level1 = $chinaObj-city[$i]["pyName"];
$shengjiURL = sprintf($URLStyle, $level1);
$shengjiStr = file_get_contents($shengjiURL);
//echo $shengjiStr;
$shengjiObj = simplexml_load_string($shengjiStr);
$shengjiObjLen = count($shengjiObj-city);
// echo $chinaObj-city[$i]["quName"];
// echo " ".$shengjiObjLen."\n";
for ($j=0;$j$shengjiObjLen;$j++){
//遍历市一级节点
$level2 = $shengjiObj-city[$j]["pyName"];
$shijiURL = sprintf($URLStyle, $level2);
$shijiStr = file_get_contents($shijiURL);
//echo $shijiStr;
$shijiObj = simplexml_load_string($shijiStr);
//直辖市和海南、台湾、 *** 等没有县级节点
if(!$shijiObj){
echo "WARNNING: not exsit next level node. - ".$level1."-".$shijiURL."\n";
echo ' "'.$shengjiObj-city[$j]["cityname"].'" = ';
echo $shengjiObj-city[$j]["url"].",\n";
continue;
}
$shijiObjLen = count($shijiObj-city);
//echo $shengjiObj-city[$j]["cityname"]." ";
//echo $shijiObjLen."\n";
for ($k=0;$k$shijiObjLen;$k++){
//遍历县一级节点
$xianji_code = $shijiObj-city[$k]["url"];
echo ' "'.$shijiObj-city[$k]["cityname"].'" = ';
echo $shijiObj-city[$k]["url"].",\n";
//echo $xianji_code."\n";
}
}
}
//print_r($chinaObj);
?
通过XML接口根节点递归获得全国几千个县以上城市cide code的代码
可以借由php的api或者preg_match_all偷偷撷取去达成目的
这里给你一段我给台湾朋友有一段源码
?php
header(\"Content-Type: text/html; charset=utf-8\");
function getWeather($city){
$toURL = \"
$city.htm\";
$post = array();
$ch = curl_init();
$options = array(
CURLOPT_REFERER='',
CURLOPT_URL=$toURL,
CURLOPT_VERBOSE=0,
CURLOPT_RETURNTRANSFER=true,
CURLOPT_USERAGENT=\"Mozilla/4.0 (compatible;)\",
CURLOPT_POST=true,
CURLOPT_POSTFIELDS=http_build_query($post),
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
curl_close($ch);
//连接中央气象局
echo 'pre';
preg_match_all('/table class=\"FcstBoxTable01\" [^]*[^]*(.*)\/div/si',$result, $matches, PREG_SET_ORDER);
preg_match_all('/td nowrap=\"nowrap\" [^]*[^]*(.*)\/td/si',$matches[0][1], $m1, PREG_SET_ORDER);
$m2 = explode('/td',$m1[0][1]);
// print_r($m2);//取得每日资料m2[0~6]
$weather = array();
for($i=0;$i=6;$i++){
preg_match_all('/src=[^]*[^](.*)/si',$m2[$i], $m5, PREG_SET_ORDER);//取得天气图档
$m6 = explode('\"',$m5[0][0]);
$wi='
($m6[1],'\.\./\.\./');
$wtitle = $m6[3];
print_r($wtitle);
$weather[$i]['date'] = date(\"m-d\", mktime(0, 0, 0, date(\"m\"), date(\"d\")+$i,date(\"Y\")));
$weather[$i]['temperature'] = trim(strip_tags($m2[$i]));
$weather[$i]['title'] = $wtitle;
$weather[$i]['img'] = $wi;
}
return($weather);
}
$weather=getWeather(\"Taipei_City\") ;
print_r($weather);
// header(\"Location:loc.php\");
?
首先
$toURL = \"\";
这里是读取资料的网址
上面的是台湾中央气象局
preg_match_all('/table class=\"FcstBoxTable01\" [^]*[^]*(.*)\/div/si',$result, $matches, PREG_SET_ORDER);
preg_match_all('/td nowrap=\"nowrap\" [^]*[^]*(.*)\/td/si',$matches[0][1], $m1, PREG_SET_ORDER);
这里是截取台湾中央气象局网页信息table class=\"FcstBoxTable01\" [^]*[^]*(.*)\/div的资料以及td nowrap=\"nowrap\" [^]*[^]*(.*)\/td的资料分别是1天跟1周
$m2 = explode('/td',$m1[0][1]);
// print_r($m2);//取得每日资料m2[0~6]
这里是取得每日的资料
preg_match_all('/src=[^]*[^](.*)/si',$m2[$i], $m5, PREG_SET_ORDER);//取得天气图档
这里是取得天气的图档
$m6 = explode('\"',$m5[0][0]);
$wi='
($m6[1],'\.\./\.\./');
$wtitle = $m6[3];
print_r($wtitle);
$weather[$i]['date'] = date(\"m-d\", mktime(0, 0, 0, date(\"m\"), date(\"d\")+$i,date(\"Y\")));
$weather[$i]['temperature'] = trim(strip_tags($m2[$i]));
$weather[$i]['title'] = $wtitle;
$weather[$i]['img'] = $wi;
这里是返回的网址,日期,标题,图档等等的资料
$weather=getWeather(\"Taipei_City\") ;
print_r($weather);
然后这里是显示出地区的一周天气预报
结论:就是如果你想从网站上面截取天气预报
在php可以是用preg_match_all(网页的表格table,表格的列数tr,表格的栏位td,或者更加广泛的标签div等等获取)
尽管未能赢得 AV-TEST 的最高防病毒(AV-Comparatives)等级评价,但 Microsoft Defender 仍是市面上最合适的产品之一。尴尬的是,在 Windows 11 Build 22581 编译版本中,许多人发现遇到了媒体播放问题。由 Reddit 网友的讨论可知,问题与系...
Microsoft Defender for Endpoint 今天出现了非常严重的误报事件,竟然将自家的 Office 应用程序更新 OfficeSvcMgr.exe 检测为勒索软件。今天早些时候,系统管理员在更新 Microsoft Defender for Endpoint 时注意到了恶意程...
近日名为 Nimbuspwn 的漏洞组合被曝光,可以让本地攻击者在 Linux 系统上提升权限,部署从后门到勒索软件等恶意软件。微软的安全研究人员在今天的一份报告中披露了这些问题,并指出它们可以被串联起来,在一个脆弱的系统上获得 root 权限。 Nimbuspwn 存在于 networkd-dis...
上周,Chrome Security 团队的 Adrian Taylor,在一篇谷歌安全博客文章中解释了“为何在野外被利用的 CVE 漏洞似乎有所增加”。对于这种漏洞利用的可见性增长趋势,归咎于多个方面的因素。而谷歌旗下的 Project Zero 团队,也有对包括 WebKit、IE、Flash、...
经历了持续一年多的追踪,网络安全研究人员终于摸清了“中东之眼”新闻网站入侵事件的来龙去脉。由 ESET 周二发布的报告可知,一群黑客入侵了总部位于伦敦的这家热门新闻网站。这家网站着眼于中东地区的新闻报道,而攻击者的最终目标却是网站访客。 伊朗驻阿布扎比大使馆网站的脚本注入 据悉,这轮黑客活动一直从...
周五晚上,一场破坏性网络攻击使巴基斯坦国家银行运营陷入瘫痪。该事件影响了该银行的自动取款机、内部网络和移动应用程序。目前,该事件被定为抹除数据的恶意软件攻击,而不是勒索软件攻击。 该事件发生在周五和周六之间的晚上,影响了银行后台系统,并影响了用于连接银行分支机构的服务器、控制银行ATM网络的后台基础...