利用weather api 获取天气预报提升网站用户体验

如果你的网站顶部有多余的空间,那么插入一段简短的天气预报,是不是能够让用户感觉一点小温馨呢,简单的一段代码就能让我们的网站具备实时天气预报的功能。今天莫晓寒就来讲述一下如何利用weather api 来实现网站自动播报天气情况。

首先你的空间要支持php,莫晓寒现在转型php了,asp的代码暂时没有研究。。。o(∩_∩)o !

 

php代码

<?php 
$url="http://m.weather.com.cn/data/101201406.html";
$content = file_get_contents($url);
echo $content;
?>

 

html代码

<html><head>
<script type="text/javascript">
var xmlhttp;if(window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}xmlhttp.onreadystatechange=function(){if(xmlhttp.readyState==4&&xmlhttp.status==200){var func=new Function("return "+xmlhttp.responseText);var jason=func();document.getElementById("weather").innerHTML="今天是:"+jason.weatherinfo.date_y+" "+jason.weatherinfo.week+" 今天的温度:"+jason.weatherinfo.temp1+" "+jason.weatherinfo.weather1+" "+jason.weatherinfo.wind1;}}
var url="weather.php";xmlhttp.open("GET",url,true);xmlhttp.send(null);
</script>
</head><body><div id="weather"></div></body></html>

演示页面:http://www.hf93.com/demo/weather.htm

你可能感兴趣的:(利用weather api 获取天气预报提升网站用户体验)