function getWeatherInfo($cityName) { if ($cityName == "") { return "发送天气+城市,例如'天气深圳'"; } $citycode = fromKeywordToCode($cityName); if ($citycode == "") { return "没有该城市?"; } $winddirection = array("0" => "无持续风向", "1" => "东北风", "2" => "东风", "3" => "东南风", "4" => "南风", "5" => "西南风", "6" => "西风", "7" => "西北风", "8" => "北风", "9" => "旋转风"); $windpower = array("0" => "微风", "1" => "3-4级", "2" => "4-5级", "3" => "5-6级", "4" => "6-7级", "5" => "7-8级", "6" => "8-9级", "7" => "9-10级", "8" => "10-11级", "9" => "11-12级"); $phenomenon = array("00" => "晴", "01" => "多云", "02" => "阴", "03" => "阵雨", "04" => "雷阵雨", "05" => "雷阵雨伴有冰雹", "06" => "雨夹雪", "07" => "小雨", "08" => "中雨", "09" => "大雨", "10" => "暴雨", "11" => "大暴雨", "12" => "特大暴雨", "13" => "阵雪", "14" => "小雪", "15" => "中雪", "16" => "大雪", "17" => "暴雪", "18" => "雾", "19" => "冻雨", "20" => "沙尘暴", "21" => "小到中雨", "22" => "中到大雨", "23" => "大到暴雨", "24" => "暴雨到大暴雨", "25" => "大暴雨到特大暴雨", "26" => "小到中雪", "27" => "中到大雪", "28" => "大到暴雪", "29" => "浮尘", "30" => "扬沙", "31" => "强沙尘暴", "53" => "霾", "99" => "无"); $weatherArray = array(); $weatherArray[] = array("Title" => $cityName . "天气预报", "Description" => "", "PicUrl" => "", "Url" => ""); //实况 $liveInfo = json_decode(getWeatherData($citycode, "observe"), true); $weatherArray[] = array("Title" => "【实况】温度" . $liveInfo["l"]["l1"] . "℃ 湿度" . $liveInfo["l"]["l2"] . "%% " . $winddirection[$liveInfo["l"]["l4"]] . $liveInfo["l"]["l3"] . "级 发布时间:" . $liveInfo["l"]["l7"], "Description" => "", "PicUrl" => "", "Url" => ""); //指数 $indexInfo = json_decode(getWeatherData($citycode, "index"), true); $indextitle = ""; for ($i = 0; $i < count($indexInfo["i"]); $i++) { $indextitle .= "【" . $indexInfo["i"][$i]["i4"] . "】" . $indexInfo["i"][$i]["i5"] . "\n"; break; } $weatherArray[] = array("Title" => trim($indextitle), "Description" => "", "PicUrl" => "", "Url" => ""); //3日 $forecast3dInfo = json_decode(getWeatherData($citycode, "forecast3d"), true); $day3Info = $forecast3dInfo["f"]["f1"]; $weekArray = array("日", "一", "二", "三", "四", "五", "六"); for ($i = 0; $i < count($day3Info); $i++) { if (date("H") < 18) { $offset = strtotime("+" . $i . " day"); $forecast3dtitle = date("m月d日", $offset) . " 周" . $weekArray[date('w', $offset)] . " " . $phenomenon[$day3Info[$i]["fa"]] . ($day3Info[$i]["fa"] != $day3Info[$i]["fb"] ? "转" . $phenomenon[$day3Info[$i]["fb"]] : "") . " " . $day3Info[$i]["fc"] . "℃~" . $day3Info[$i]["fd"] . "℃ " . $winddirection[$day3Info[$i]["fe"]] . " " . $windpower[$day3Info[$i]["fg"]] . " " . "日出日落:" . str_replace("|", "~", $day3Info[$i]["fi"]); $picurl = "http://discuz.comli.com/weixin/weather/icon/d" . $day3Info[$i]["fa"] . ".jpg"; } else { $offset = strtotime("+" . ($i + 1) . " day"); $forecast3dtitle = date("m月d日", $offset) . " 周" . $weekArray[date('w', $offset)] . " " . $phenomenon[$day3Info[$i]["fb"]] . " " . $day3Info[$i]["fd"] . "℃" . $winddirection[$day3Info[$i]["fe"]] . " " . $windpower[$day3Info[$i]["fg"]] . " " . "日出日落:" . str_replace("|", "~", $day3Info[$i]["fi"]); $picurl = "http://discuz.comli.com/weixin/weather/icon/d" . $day3Info[$i]["fb"] . ".jpg"; } $weatherArray[] = array("Title" => trim($forecast3dtitle), "Description" => "", "PicUrl" => $picurl, "Url" => ""); } return $weatherArray; }
while (!feof($fdCacheFile)) { $arrCacheData[] = stream_get_line($fdCacheFile, 4096, "\n"); } fclose($fdCacheFile); $strWeatherData = implode("\r\n", $arrCacheData); $strWeatherData = strip_tags(str_replace(array(',', "\r\n"), array('', ','), $strWeatherData)); $arrCacheData = str_getcsv($strWeatherData); return array_chunk($arrCacheData, 3); } date_default_timezone_set('EST'); $strWeatherDataBaseURL = 'http://forecast.weather.gov/MapClick.php?TextType=3'; $strWeatherIconBaseURL = 'http://mobile.wrh.noaa.gov/weather/images/fcicons/'; if (DEBUG) { header('content-type: text/plain'); } else { header('content-type: text/html'); } if (!defined('WRAPPER_ACTIVE')) { include 'WaterWeatherDefines.php'; include 'WeatherVariables.php'; } if ($lat != "nolat" && $long != "nolong") { $strWeatherDataURL = $strWeatherDataBaseURL . '&textField1=' . $lat . '&textField2=' . $long; $strCacheFile = $strCacheBase . 'NOAA-' . md5($lat . $long); $timeLimit = $timeLimitWeather; $strGeographicLocation = ''; $arrWeatherData = getWeatherData($strWeatherDataURL, $strCacheFile, $timeLimit, $strGeographicLocation); if (!defined('WRAPPER_ACTIVE')) { include 'WeatherOutput.php'; } }
<?php /** * widget.php */ header('Content-type: application/javascript'); require 'weather.php'; // Load supporting code $weather = getWeatherData($_GET['zip']); $location = $weather['location']; $temperature = $weather['temperature']; $desc = $weather['desc']; // Must qualify full path for asset urls; relative // urls won't work, as they'll point to the publisher's server $image = preg_replace('/[^\/]+$/', '', $_SERVER['REQUEST_URI']) . $weather['image']; // Output JavaScript to page echo <<<EOD document.write('\ <div>\ <p>$location</p>\ <img src="$image"/>\ <p><strong>$temperature °F</strong> — $desc</p>\ </div>'); EOD;