Storm1
Storm1

Reputation: 209

how to get data from xml?

I am trying to get weather data from this XML: https://www.yr.no/sted/Norge/Vestland/Bergen/Bergen/varsel.xml

I would like to display like this:

enter image description here

I have no idea where to start.

    $url = ('');

    function Feed($url) {
      $feed = simplexml_load_file($url) or die('Can not connect to server');
      $result = array();
      foreach ($feed->channel->item as $content) {
          array_push($result, $content);
      }
    }
    ?>

Found an example ^ but did'nt get it to work... Quite unexperienced, any help is appreciated.

Upvotes: 1

Views: 77

Answers (1)

Nigel Ren
Nigel Ren

Reputation: 57131

The main part is extracting the correct parts from the content you get back, following the XML you show in the linked page - the following code extracts most of the details you want.

$feed = simplexml_load_file($url) or die('Can not connect to server');
$result = array();
foreach ($feed->forecast->tabular->time as $content) {
    array_push($result, [ "from" => (string)$content['from'], 
        "to" => (string)$content['to'],
        'symbol' => (string)$content->symbol['name'],
        'temperature' => (string)$content->temperature['value'],
        'windDirection' => (string)$content->windDirection['code'],
        'windSpeed' => (string)$content->windSpeed['mps'],
    ]);
}

How you present them is now up to you.

Upvotes: 1

Related Questions