1

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you. when I use it code it doesn't display anything? do I need to use return or something? I'm trying to learn
If you use it in your code, return $result; or just print_r($result);.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.