已掉线,重新登录

首页 > 绿虎论坛 > 历史版块 > 编程 > PHP > 源码

标题: 一个从新浪获取当天天气的函数

作者: @Ta

时间: 2014-11-18

点击: 2202

核心代码:
function sina_weather($city){
    // 天气地址
    $url = 'http://php.weather.sina.com.cn/iframe/index/w_cl.php?code=js&day=0&city='. $city .'&dfc=1&charset=utf-8';
    // 从新浪获取天气数据
    $file = file_get_contents($url);
    // 从获取的数据中找到城市名
    preg_match_all("@\['(.*)'\]@",$file,$city);
    // 把城市名添加到数组当中
    $weather['city'] = $city[1][0];
    // 从获取的数据中找到天气数据
    preg_match_all("@:'(.*?)'@s",$file,$data);
    // 从新排列所找到的天气数据
    list($weather['s1'], $weather['s2'], $weather['f1'], $weather['f2'], $weather['t1'], $weather['t2'], $weather['p1'], $weather['p2'], $weather['d1'], $weather['d2'], $weather['now'], $weather['time'], $weather['update'], $weather['error'], $weather['total']) = $data[1];
    // 函数返回结果
    return $weather;
}

返回数组:Array
(
    [city] => 北京
    [total] => 1
    [error] => 0
    [update] => 北京时间11月18日17:05更新
    2025-07-05 22:52:15 => 1416317968
    [now] => 2014-11-18 21:39:28
    [d2] => 无持续风向
    [d1] => 无持续风向
    [p2] => ≤3
    [p1] => ≤3
    [t2] => 0
    [t1] => 13
    [f2] => duoyun
    [f1] => qing
    [s2] => 多云
    [s1] => 晴
)
只能查询当天的天气~
简陋调用程序:http://pan.baidu.com/s/1o6MCCQ2|演示
返回值祥解:http://blog.ll00.cn/post-7.html

[隐藏样式|查看源码]


『回复列表(3|隐藏机器人聊天)』

1. 不错呦!  支持一个。
(/@Ta/2014-11-18 22:05//)

2. 感谢分享
(/@Ta/2014-11-18 23:30//)

3. 好看
(/@Ta/2014-11-20 07:09//)

回复需要登录

7月5日 22:52 星期六

本站由hu60wap6驱动

备案号: 京ICP备18041936号-1