PHP变量$url_icon与$icon_weather未更新问题求助
Hey there! Let's work through this icon problem together—since you're new to PHP, I'll break this down into actionable steps so you can fix it quickly.
First, let's clarify what's likely going wrong: your $url_icon and $icon_weather variables probably aren't being assigned correctly, or you're missing a key mapping step between OpenWeatherMap's icon codes and the Weather Icons class names you're using in the frontend.
Step 1: Verify You're Getting Valid Data from the API
Before anything else, let's confirm you're actually pulling the right data from OpenWeatherMap. Add this debug code right after you fetch $data_weather:
// Print the full weather object to check its structure var_dump($data_weather->weather[0]); die; // Pause the script to view the output
You should see an object with properties like icon (e.g., "01d"), description (e.g., "clear sky"), etc. If this doesn't show up, your API response parsing might be broken (double-check how you're decoding the JSON—make sure you're using json_decode($response, false) to get an object, not an array).
Step 2: Correctly Assign Your Variables with Icon Mapping
OpenWeatherMap returns short icon codes (like "01d"), but Weather Icons uses full class names (like "wi-day-sunny"). You need to map these codes to the right class names. Here's how to do it properly:
// First, extract the raw icon code and description from the API data $api_icon_code = $data_weather->weather[0]->icon; $icon_weather = $data_weather->weather[0]->description; // This gets the text description // Create a mapping between OpenWeather's codes and Weather Icons classes $icon_mapping = [ '01d' => 'wi-day-sunny', '01n' => 'wi-night-clear', '02d' => 'wi-day-cloudy', '02n' => 'wi-night-alt-cloudy', '03d' => 'wi-cloud', '03n' => 'wi-cloud', '04d' => 'wi-cloudy', '04n' => 'wi-cloudy', '09d' => 'wi-day-showers', '09n' => 'wi-night-alt-showers', '10d' => 'wi-day-rain', '10n' => 'wi-night-alt-rain', '11d' => 'wi-day-thunderstorm', '11n' => 'wi-night-alt-thunderstorm', '13d' => 'wi-day-snow', '13n' => 'wi-night-alt-snow', '50d' => 'wi-day-fog', '50n' => 'wi-night-fog' ]; // Get the correct class name, or fall back to a "not available" icon if no match $url_icon = isset($icon_mapping[$api_icon_code]) ? $icon_mapping[$api_icon_code] : 'wi-na';
If you were just assigning $url_icon = $data_weather->weather[0]->icon; before, that's why your icons weren't showing—you were using the raw code instead of the proper Weather Icons class name.
Step 3: Check Variable Scope
Make sure your $url_icon and $icon_weather variables are accessible where you're rendering the frontend. For example:
- If you defined these variables inside a function, you need to return them or pass them to the part of your script that outputs the HTML.
- Don't wrap them in a conditional that never runs (double-check if your API call is successful before assigning variables).
Step 4: Confirm Weather Icons CSS is Loaded
Even if your variables are correct, the icons won't show if you haven't included the Weather Icons stylesheet. Add this to the <head> section of your HTML:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/weather-icons/2.0.12/css/weather-icons.min.css">
Once you've gone through these steps, your frontend code:
<div><i class="wi <?= $url_icon?>"></i></div> <div><?= $icon_weather ?></div>
Should correctly display the matching icon and weather description.
内容的提问来源于stack exchange,提问作者SVN7H

