在 PHP 中使用 OpenWeatherMap 预报 API
Using OpenWeatherMap forecast API in PHP
我正在尝试显示来自 openweathermap 的城市预报。
但是我的 foreach 什么也没显示。怎么了?
<?php
$url = "http://api.openweathermap.org/data/2.5/forecast?zip=85080,de&lang=de&APPID=MYKEY";
$contents = file_get_contents($url);
$clima = json_decode($contents, true);
foreach($clima as $data) {
echo $data->list->main->temp_min;
}
?>
json_decode(string, true)
的结果是一个关联数组。
<?php
$url = "http://api.openweathermap.org/data/2.5/forecast?zip=85080,de&lang=de&APPID=MYKEY";
$contents = file_get_contents($url);
$clima = json_decode($contents, true);
foreach($clima['list'] as $data) {
echo $data['main']['temp_min'];
}
?>
如果要使用对象语法,请不要将 associative 设置为 true
。
$clima = json_decode($contents);
foreach($clima->list as $data) {
echo $data->main->temp_min;
}
您使用了 json_decode
参数 associative 为真。
所以 $data 更像是一个数组而不是对象。
基于 sample (similar to your url), you should access your values with square bracket syntax :
$data['main']['temp_min'];
让我们试试...
<?php
$city = 'Dhaka';
$country = 'BD';
$url = 'http://api.openweathermap.org/data/2.5/forecast/daily?q=' . $city . ',' . $country . '&units=metric&cnt=7&lang=en&appid=c0c4a4b4047b97ebc5948ac9c48c0559';
$json = file_get_contents( $url );
$data = json_decode( $json, true );
$data['city']['name'];
// var_dump($data );
foreach ( $data['list'] as $day => $value ) {
echo 'Max temperature for day ' . $day
. ' will be ' . $value['temp']['max'] . '<br />';
echo '<img src="http://openweathermap.org/img/w/' . $value['weather'][0]['icon'] . '.png"
class="weather-icon" />';
}
我正在尝试显示来自 openweathermap 的城市预报。 但是我的 foreach 什么也没显示。怎么了?
<?php
$url = "http://api.openweathermap.org/data/2.5/forecast?zip=85080,de&lang=de&APPID=MYKEY";
$contents = file_get_contents($url);
$clima = json_decode($contents, true);
foreach($clima as $data) {
echo $data->list->main->temp_min;
}
?>
json_decode(string, true)
的结果是一个关联数组。
<?php
$url = "http://api.openweathermap.org/data/2.5/forecast?zip=85080,de&lang=de&APPID=MYKEY";
$contents = file_get_contents($url);
$clima = json_decode($contents, true);
foreach($clima['list'] as $data) {
echo $data['main']['temp_min'];
}
?>
如果要使用对象语法,请不要将 associative 设置为 true
。
$clima = json_decode($contents);
foreach($clima->list as $data) {
echo $data->main->temp_min;
}
您使用了 json_decode
参数 associative 为真。
所以 $data 更像是一个数组而不是对象。
基于 sample (similar to your url), you should access your values with square bracket syntax :
$data['main']['temp_min'];
让我们试试...
<?php
$city = 'Dhaka';
$country = 'BD';
$url = 'http://api.openweathermap.org/data/2.5/forecast/daily?q=' . $city . ',' . $country . '&units=metric&cnt=7&lang=en&appid=c0c4a4b4047b97ebc5948ac9c48c0559';
$json = file_get_contents( $url );
$data = json_decode( $json, true );
$data['city']['name'];
// var_dump($data );
foreach ( $data['list'] as $day => $value ) {
echo 'Max temperature for day ' . $day
. ' will be ' . $value['temp']['max'] . '<br />';
echo '<img src="http://openweathermap.org/img/w/' . $value['weather'][0]['icon'] . '.png"
class="weather-icon" />';
}