我有这个天气脚本,并且我试图为每个天气条件(案例)显示不同的图像,如果是白天或黑夜,图像也会有所不同。
这就是我正在做的......
获取XML信息。进入.PHP
<?php
$CapDCityANDState = "Atlanta, GA";
$url="https://www.google.com/ig/api?weather=$CapDCityANDState&hl=en&referrer=googlecalendar";
$xml = simplexml_load_file($url);
//print_r($xml);
//echo "Test: " . $xml->current_conditions->temp_f['data'];
$DayNow = date(D);
$DayTomorrow = date('D', strtotime('+1 day'));
$DayAfterTomorrow = date('D', strtotime('+2 day'));
$DayAfterAfterTomorrow = date('D', strtotime('+3 day'));
$CurrentTxt = $xml->weather->current_conditions->condition->attributes()->data;
$CurrentTemp = $xml->weather->current_conditions->temp_f->attributes()->data;
//echo $CurrentTemp[0];
echo("<BR><BR>");
$DayOneLow = $xml->weather->forecast_conditions[0]->low->attributes()->data;
$DayOneHi = $xml->weather->forecast_conditions[0]->high->attributes()->data;
$DayOneTxt = $xml->weather->forecast_conditions[0]->condition->attributes()->data;
//echo $DayOneLow[0];
$DayTwoLow = $xml->weather->forecast_conditions[1]->low->attributes()->data;
$DayTwoHi = $xml->weather->forecast_conditions[1]->high->attributes()->data;
$DayTwoTxt = $xml->weather->forecast_conditions[1]->condition->attributes()->data;
//echo $DayTwoLow[0];
$DayTriLow = $xml->weather->forecast_conditions[2]->low->attributes()->data;
$DayTriHi = $xml->weather->forecast_conditions[2]->high->attributes()->data;
$DayTriTxt = $xml->weather->forecast_conditions[2]->condition->attributes()->data;
//echo $DayTriLow[0];
$DayFourLow = $xml->weather->forecast_conditions[3]->low->attributes()->data;
$DayFourHi = $xml->weather->forecast_conditions[3]->high->attributes()->data;
$DayFourTxt = $xml->weather->forecast_conditions[3]->condition->attributes()->data;
//echo $DayFourLow[0];
?>
显示天气预报信息。
<div>Currently: <?php echo $CurrentTemp[0]; ?>° - <?php echo $CurrentTxt[0]; ?> - H: <?php echo $DayOneHi[0]; ?>° L: <?php echo $DayOneLow[0]; ?>° - Rest of the Day: <?php echo $DayOneTxt[0]; ?></div>
<div><?php echo($DayTomorrow);?>: <?php echo $DayTwoHi[0]; ?>°/<?php echo $DayTwoLow[0]; ?>° - <?php echo $DayTwoTxt[0]; ?></div>
<div><?php echo($DayAfterTomorrow);?>: <?php echo $DayTriHi[0]; ?>°/<?php echo $DayTriLow[0]; ?>° - <?php echo $DayTriTxt[0]; ?></div>
<div><?php echo($DayAfterAfterTomorrow);?>: <?php echo $DayFourHi[0]; ?>°/<?php echo $DayFourLow[0]; ?>° - <?php echo $DayFourTxt[0]; ?></div>
<br>
发现是当天还是现在
<?php
$img = time() > $sunriseTime && time() < $sunsetTime ? 'day' : 'nite';
// $img <-- will say 'day' if it's day and 'nite' if its nite
?>
获取图像显示依赖于它是否为天或同时获取天气条件文本并将图像基于这两个因素。
<?php
if ($img == "day"){
// START OF DAY
switch ($DayOneTxt[0]) {
case "CLEAR":
$WeatherIMG = "1.png";
break;
case "MOSTLY CLOUDY":
$WeatherIMG = "4.png";
break;
case "SUNNY":
$WeatherIMG = "2.png";
break;
case "MOSTLY SUNNY":
$WeatherIMG = "2.png";
break;
case "PARTLY CLOUDY":
$WeatherIMG = "3.png";
break;
case "PARTLY SUNNY":
$WeatherIMG = "3.png";
break;
case "HAZE":
$WeatherIMG = "5.png";
break;
case "DUST":
$WeatherIMG = "5.png";
break;
case "FOG":
$WeatherIMG = "5.png";
break;
case "CHANCE OF STORM":
$WeatherIMG = "6.png";
break;
case "OVERCAST":
$WeatherIMG = "8.png";
break;
case "CHANCE OF RAIN":
$WeatherIMG = "8.png";
break;
case "CLOUDY":
$WeatherIMG = "11.png";
break;
case "RAIN":
$WeatherIMG = "12.png";
break;
case "STORM":
$WeatherIMG = "12.png";
break;
case "MIST":
$WeatherIMG = "14.png";
break;
case "LIGHT RAIN":
$WeatherIMG = "14.png";
break;
case "SCATTERED SHOWERS":
$WeatherIMG = "14.png";
break;
case "SHOWERS":
$WeatherIMG = "14.png";
break;
case "THUNDERSTORM":
$WeatherIMG = "15.png";
break;
case "SCATTERED THUNDERSTORMS":
$WeatherIMG = "16.png";
break;
case "CHANCE OF TSTORM":
$WeatherIMG = "17.png";
break;
case "FLURRIES":
$WeatherIMG = "21.png";
break;
case "LIGHT SNOW":
$WeatherIMG = "21.png";
break;
case "SCATTERED SNOW SHOWERS":
$WeatherIMG = "21.png";
break;
case "SNOW SHOWERS":
$WeatherIMG = "22.png";
break;
case "CHANCE OF SNOW":
$WeatherIMG = "23.png";
break;
case "SNOW":
$WeatherIMG = "27.png";
break;
case "RAIN AND SNOW":
$WeatherIMG = "29.png";
break;
case "ICE/SNOW":
$WeatherIMG = "29.png";
break;
case "SLEET":
$WeatherIMG = "29.png";
break;
case "ICY":
$WeatherIMG = "31.png";
break;
case "FREEZING DRIZZLE":
$WeatherIMG = "31.png";
break;
case "SMOKE":
$WeatherIMG = "32.png";
break;
case "WINDY":
$WeatherIMG = "32.png";
break;
}
// END OF DAY
} else {
// START OF NITE
switch ($DayOneTxt[0]) {
case "PARTLY SUNNY":
$WeatherIMG = "33.png";
break;
case "SCATTERED THUNDERSTORMS":
$WeatherIMG = "42.png";
break;
case "SHOWERS":
$WeatherIMG = "40.png";
break;
case "SCATTERED SHOWERS":
$WeatherIMG = "39.png";
break;
case "RAIN AND SNOW":
$WeatherIMG = "29.png";
break;
case "OVERCAST":
$WeatherIMG = "8.png";
break;
case "LIGHT SNOW":
$WeatherIMG = "22.png";
break;
case "FREEZING DRIZZLE":
$WeatherIMG = "31.png";
break;
case "CHANCE OF RAIN":
$WeatherIMG = "39.png";
break;
case "SUNNY":
$WeatherIMG = "33.png";
break;
case "CLEAR":
$WeatherIMG = "33.png";
break;
case "MOSTLY SUNNY":
$WeatherIMG = "34.png";
break;
case "PARTLY CLOUDY":
$WeatherIMG = "35.png";
break;
case "MOSTLY CLOUDY":
$WeatherIMG = "36.png";
break;
case "CHANCE OF STORM":
$WeatherIMG = "42.png";
break;
case "RAIN":
$WeatherIMG = "40.png";
break;
case "CHANCE OF SNOW":
$WeatherIMG = "44.png";
break;
case "CLOUDY":
$WeatherIMG = "36.png";
break;
case "MIST":
$WeatherIMG = "39.png";
break;
case "STORM":
$WeatherIMG = "40.png";
break;
case "THUNDERSTORM":
$WeatherIMG = "41.png";
break;
case "CHANCE OF TSTORM":
$WeatherIMG = "42.png";
break;
case "SLEET":
$WeatherIMG = "29.png";
break;
case "SNOW":
$WeatherIMG = "43.png";
break;
case "ICY":
$WeatherIMG = "31.png";
break;
case "DUST":
$WeatherIMG = "32.png";
break;
case "FOG":
$WeatherIMG = "32.png";
break;
case "SMOKE":
$WeatherIMG = "32.png";
break;
case "HAZE":
$WeatherIMG = "32.png";
break;
case "FLURRIES":
$WeatherIMG = "25.png";
break;
case "LIGHT RAIN":
$WeatherIMG = "39.png";
break;
case "SNOW SHOWERS":
$WeatherIMG = "44.png";
break;
case "ICE/SNOW":
$WeatherIMG = "43.png";
break;
case "WINDY":
$WeatherIMG = "32.png";
break;
case "SCATTERED SNOW SHOWERS":
$WeatherIMG = "43.png";
break;
}
// END OF NITE
}
?>
显示图像名称使用例如 "32.png"
<?php echo($WeatherIMG); ?>
图片始终显示"1.png"
,这是错误的:(
答案 0 :(得分:1)
<?php
$txtDay = Array( "clear"=>1, "sunny"=>2, "partly cloudy"=>3, "mostly cloudy"=>4, "haze"=>5, "fog"=>5, "dust"=>5, ............. "scatter snow showers"=>43);
$txtNight = Array( "clear"=>1, "sunny"=>2, "partly cloudy"=>3, "mostly cloudy"=>4, "haze"=>5, "fog"=>5, "dust"=>5, ............. "scatter snow showers"=>43);
if ($img == "day"){
$imgWeather = $txtDay[{$DayOneTxt[0]}] . ".png";
} else {
$imgWeather = $txtNight[{$DayOneTxt[0]}] . ".png";
}
?>