Jan 18

php几行代码搞定建站的天气预报代码 夜晚

vwen , 19:55 , 网络编程 » PHP编程 , 评论(0) , 引用(0) , 阅读(584) , Via 本站原创 | |
开发网站时经常会使用到某个城市的天气情况,我们公司的美工在做企业站点时就会在页面上体现企业本地的天气状况。现在就利用php的file_get_contents函数快速获取雅虎网站一样的3天天气情况:

<?php
$cityname="福州";
$gethtml=@file_get_contents("http://weather.cn.yahoo.com/weather.html?city=".urlencode(mb_convert_encoding($cityname, 'UTF-8','GB2312'))."");
preg_match_all("/<span class=\"f14\"><strong>.+<\/span>/",$gethtml,$matches);
print_r($matches);
?>

$matches是一个数组,$matches[0][0]就是当前的天气了。总共返回3天的天气情况数据。很简单吧。再配合ajax做个加载效果挺不错的。
Tags:
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]