PHP 从 api.met.no 读取天气信息

我正在尝试从此https://api.met.no/weatherapi/locationforecast/2.0/compact?lat=-16.516667&lon=-68.166667读取 JSON

$options = array(

    'http'=>array(

      'method'=>"GET",

      'header'=>"Accept-language: en\r\n" .

                "User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10\r\n" // i.e. An iPad 

    )

);  


$context = stream_context_create($options);

$data_url = "https://api.met.no/weatherapi/locationforecast/2.0/compact?lat=-16.516667&lon=-68.166667";


$json = file_get_contents($data_url,false,$context);


$data = new RecursiveIteratorIterator(

    new RecursiveArrayIterator(json_decode($json, TRUE)),

    RecursiveIteratorIterator::SELF_FIRST);



foreach ($data as $key => $val) {

    if(is_array($val)) {

        echo "$key:\n<br>";

    } else {

        echo "$key => $val\n<br>";

    }

}

这工作正常,但我需要根据键选择特定值,例如:


foreach ($data as $key) {

      echo $key["properties"] ["timeseries"] ["0-x"] ["data"] ["instant"] ["details"];

}

但这当然对我不起作用。请问不知道怎么办?谢谢


三国纷争
浏览 96回答 1
1回答

精慕HU

查看原始 json 返回,您尝试获取的项目相当隐蔽,但您仍然可以轻松获取它。数据返回示例:{&nbsp; "type": "Feature",&nbsp; "geometry": {&nbsp; &nbsp; &nbsp;...&nbsp; },&nbsp; "properties": {&nbsp; &nbsp; "meta": {&nbsp; &nbsp; &nbsp; &nbsp;...&nbsp; &nbsp; },&nbsp; &nbsp; "timeseries": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "time": "2020-08-18T12:00:00Z",&nbsp; &nbsp; &nbsp; &nbsp; "data": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "instant": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "details": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "air_pressure_at_sea_level": 1018.4,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "air_temperature": 4.7,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "cloud_area_fraction": 92.2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "relative_humidity": 59.3,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "wind_from_direction": 308.4,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "wind_speed": 3.8&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;},&nbsp; &nbsp; ...您可以通过以下方式访问该特定数据:$json = file_get_contents($data_url,false,$context);// This is all you need to turn json into a usable array$data = json_decode($json,true);&nbsp;// Loop on the nested timeseries group:foreach($data['properties']['timeseries'] as $ts) {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // the time part of it&nbsp; &nbsp; $time&nbsp; &nbsp; = $ts['time'];&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // get at it direct&nbsp; &nbsp; $var&nbsp; &nbsp; &nbsp;= $ts['data']['instant']['details']['air_temperature'];&nbsp; &nbsp; // shorthand it if you wish:&nbsp; &nbsp; $details = $ts['data']['instant']['details'];&nbsp; &nbsp; $var&nbsp; &nbsp; &nbsp;= $details['air_temperature'];&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // do whatever else you need to do with it&nbsp; &nbsp; echo $var;&nbsp; &nbsp; $array_of_temps[] = $var;&nbsp; &nbsp; // etc ...}
打开App,查看更多内容
随时随地看视频慕课网APP