吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3678|回复: 7
收起左侧

[其他转载] 一个从新浪获取当天天气的函数

[复制链接]
xchener 发表于 2014-11-20 23:34 来自手机
核心代码:
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更新
    2014-11-20 23:33:53 => 1416317968
    [now] => 2014-11-18 21:39:28
    [d2] => 无持续风向
    [d1] => 无持续风向
    [p2] => ≤3
    [p1] => ≤3
    [t2] => 0
    [t1] => 13
    [f2] => duoyun
    [f1] => qing
    [s2] => 多云
    [s1] => 晴
)
只能查询当天的天气~

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Scar-疤痕 发表于 2014-11-20 23:42 来自手机
谢谢楼主分享!
31412105 发表于 2014-11-20 23:43
hrbsxk 发表于 2014-11-20 23:50
wanmei 发表于 2014-11-21 00:12
谢谢分享  
77209341 发表于 2014-11-21 00:16
很好。以前做站时也找到过。
头像被屏蔽
qq1292486987 发表于 2014-11-21 01:22
提示: 作者被禁止或删除 内容自动屏蔽
 楼主| xchener 发表于 2014-12-16 13:07

php语言哈
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-9-22 15:51

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表