現在、Yahoo Weather からデータを取得しており、これまでに必要な要素を表示できます。条件コード (数値を整数に変換する必要があると思います) を使用して文字列を作成し、独自のカスタム アイコンを表示する方法に困惑しています。私が追跡している地域の天気はかなり穏やかなので、少数のアイコンのみを割り当て、それぞれが一連の状態コードをカバーするようにします。これまでのところ、これは機能している私のコードです–</p>
function bm_getWeather ($code = '', $temp = 'c') {
$file = 'http://weather.yahooapis.com/forecastrss?w=' . $code . '&u=' . $temp;
$request = new WP_Http;
$result = $request->request($file);
if (isset($result->errors)) {
return FALSE;
}
$data = $result['body'];
$output = array (
'temperature' => bm_getWeatherProperties('temp', $data),
'weather_code' => bm_getWeatherProperties('code', $data),
'class' => bm_getWeatherProperties('code', $data),
'weather' => bm_getWeatherProperties('text', $data),
);
return $output;
}
関数weather_icon() {
$data = bm_getWeather($code = '', $temp = 'c');
// Error is here
$nums = (int)$data['weather_code']; // Needs to be cast as an integer
$severe = array(12, 13, 14, 16, 19);
$rain = array(3200);
// Therefore the maths is wrong
switch($nums) {
case (in_array($nums, $severe)):
$cat = 'severe';
break;
case (in_array($nums,$rain)):
$cat = 'snow';
break;
default:
$cat = 'happy';
break;
}
return $cat;
}
function bm_getWeatherProperties ($needle, $data) {
$regex = '<yweather:condition.*' . $needle . '="(.*?)".*/>';
preg_match($regex, $data, $matches);
return (string)$matches[1];
}
私のphpスキルは今のところ十分ではないので、どんな助けも大歓迎です。