PHP-php 城市天气预报功能

需求定制 需求定制 主题:1050 回复:2283

PHP-php 城市天气预报功能

清晨说ぺ晚安 发布于 2017-10-24 字数 479 浏览 1236 回复 1

谁能给个用php封装好的函数或类来获取某一城市的天气信息数据,比如:

function getWeather($city)
{
}
class Weather
{
}
//通过函数实现
$beijing = getWeather('北京');
$haidian = getWeather('海淀');//能具体到区(或县)
//通过类实现
$weatherObj = new Weather();

$beijing = $weatherObj->getWeather('北京');
$haidian = $weatherObj->getWeather('海淀');//能具体到区(或县)

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

支持 Markdown 语法,需要帮助?

评论(1

泛泛之交 2017-10-27 1 楼

已经调试通过

<?php

//中国城市网天气代码 http://wenku.baidu.com/view/06081fc55fbfc77da269b1ec.html

class Weather
{
function getWeather($city){
$wcity = file_get_contents("wcity.txt");
//101010100=北京
//101010200=海淀
$data_url = 'http://m.weather.com.cn/data/';
$ext = '.html';
$pattern = '/([0-9]+)='.iconv("utf8","gb2312",$city).'/';
preg_match($pattern, $wcity, $matches, PREG_OFFSET_CAPTURE);
$cityID = $matches[1][0];
$data = file_get_contents($data_url.$cityID.$ext);
print_r(json_decode($data));
}
}

function getWeather($city)
{
$wcity = file_get_contents("wcity.txt");
//101010100=北京
//101010200=海淀
$data_url = 'http://m.weather.com.cn/data/';
$ext = '.html';
$pattern = '/([0-9]+)='.iconv("utf8","gb2312",$city).'/';
preg_match($pattern, $wcity, $matches, PREG_OFFSET_CAPTURE);
$cityID = $matches[1][0];
$data = file_get_contents($data_url.$cityID.$ext);
print_r(json_decode($data));
}
$beijing = getWeather('北京');
/**

  • stdClass Object
    (
    [weatherinfo] => stdClass Object
    (
    [city] => 北京
    [city_en] => beijing
    [date_y] => 2012年9月26日
    [date] =>
    [week] => 星期三
    [fchh] => 11
    [cityid] => 101010100
    [temp1] => 26℃~15℃
    [temp2] => 23℃~14℃
    [temp3] => 22℃~13℃
    [temp4] => 25℃~12℃
    [temp5] => 26℃~14℃
    [temp6] => 25℃~15℃
    [tempF1] => 78.8℉~59℉
    [tempF2] => 73.4℉~57.2℉
    [tempF3] => 71.6℉~55.4℉
    [tempF4] => 77℉~53.6℉
    [tempF5] => 78.8℉~57.2℉
    [tempF6] => 77℉~59℉
    [weather1] => 晴转阴
    [weather2] => 阴转多云
    [weather3] => 晴
    [weather4] => 晴
    [weather5] => 晴
    [weather6] => 晴转多云
    [img1] => 0
    [img2] => 2
    [img3] => 2
    [img4] => 1
    [img5] => 0
    [img6] => 99
    [img7] => 0
    [img8] => 99
    [img9] => 0
    [img10] => 99
    [img11] => 0
    [img12] => 1
    [img_single] => 0
    [img_title1] => 晴
    [img_title2] => 阴
    [img_title3] => 阴
    [img_title4] => 多云
    [img_title5] => 晴
    [img_title6] => 晴
    [img_title7] => 晴
    [img_title8] => 晴
    [img_title9] => 晴
    [img_title10] => 晴
    [img_title11] => 晴
    [img_title12] => 多云
    [img_title_single] => 晴
    [wind1] => 微风
    [wind2] => 北风5-6级
    [wind3] => 北风4-5级转微风
    [wind4] => 微风
    [wind5] => 微风
    [wind6] => 微风
    [fx1] => 微风
    [fx2] => 微风
    [fl1] => 小于3级
    [fl2] => 5-6级
    [fl3] => 4-5级转小于3级
    [fl4] => 小于3级
    [fl5] => 小于3级
    [fl6] => 小于3级
    [index] => 暖
    [index_d] => 较凉爽,建议着长袖衬裤等春秋过渡装。体弱者宜着长袖衬衫和马甲。但昼夜温差较大,请适当增减衣服。
    [index48] => 暖
    [index48_d] => 较凉爽,建议着长袖衬衫加单裤等春秋过渡装。年老体弱者宜着针织长袖衬衫、马甲和长裤。
    [index_uv] => 中等
    [index48_uv] => 最弱
    [index_xc] => 较不宜
    [index_tr] => 适宜
    [index_co] => 较舒适
    [st1] => 25
    [st2] => 14
    [st3] => 22
    [st4] => 11
    [st5] => 21
    [st6] => 15
    [index_cl] => 适宜
    [index_ls] => 适宜
    [index_ag] => 易发
    )

)
*/
$haidian = getWeather('海淀');//能具体到区(或县)

$weatherObj = new Weather();

$beijing = $weatherObj->getWeather('北京');
$haidian = $weatherObj->getWeather('海淀');//能具体到区(或县)