2

I'm new to stack overflow but I have been so lost and flustered by this issue I just needed ask. I'm attempting to parse this XML: http://www.wrh.noaa.gov/forecast/xml/xml.php?duration=168&interval=6&lat=39.9233&lon=-111.883 using this php code:

$xml = new DOMDocument();
$xml->substituteEntities = true;    
$xml->loadXML(file_get_contents($url));`

Then I'm printing out the $XML using:

echo "<br>XML START:<br>";
 echo "<pre>";
 print_r($xml);
 echo "<br>XML END<br>";
 echo "</pre>";

I have used this code with other URLs and it has always worked, so I don't understand why it doesn't work with this specific URL I would really appreciate some feedback.

3
  • I've tried it and seems to work. Only problem is that the XML contains an item 'textContent' with text 4790 characters long. I'm missing the point, or what is the goal you want to achieve? Commented May 12, 2016 at 18:06
  • Whenever I try this my 'textContent' is always empty. I need to pull the precip and relative humidity out of this XML file. I'm a little new to php so if you have any other ways this might work better im completely open to whatever Commented May 12, 2016 at 18:12
  • Added a foreach loop to see all the data. Commented May 12, 2016 at 20:02

1 Answer 1

3

The first two commented line failed with a forbidden error

noaa returned a 403 forbidden http status on the request from file_get_contents and simplexml_load_file() but FireFox could get it just fine.

So I created a curl request that looks just like FireFox.

Then I converted the XML to an array. I prefer working with arrays rather than objects.

<?php
header('Content-Type: text/plain; charset=utf-8');
$url = 'http://www.wrh.noaa.gov/forecast/xml/xml.php?duration=168&interval=6&lat=39.9233&lon=-111.883';


//$xml =  simplexml_load_file($url);
//$xml = get_file_contents($url);


$request[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$request[] = 'User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:48.0) Gecko/20100101 Firefox/48.0';
$request[] = 'Accept-Language: en-US,en;q=0.5';
$request[] = 'DNT: 1';
$request[] = 'Connection: keep-alive';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($ch);
$xml = simplexml_load_string($xml);
XMLtoArray($xml,$res);
var_export($res);

function XMLtoArray($obj, &$result) { 
  $data = $obj;
  if (is_object($data)) {
    $data = get_object_vars($data);
  }
  if (is_array($data)) {
    foreach ($data as $key => $value) {
      $res = null;
      XMLtoArray($value, $res);
      if (($key == '@attributes') && ($key)) {
        $result = $res;
      } else {
        $result[$key] = $res;
      }
    }
  } else {
    $result = $data;
  }
}
?>

This gave this:

array (
  'duration' => '168',
  'forecastCreationTime' => 'Thu May 12 16:32:00 2016 UTC',
  'latitude' => '39.933
',
  'longitude' => '-111.882
',
  'elevation' => '4685
',
  'location' => '2 Miles SSE Goshen UT',
  'interval' => '6',
  'forecastDay' => 
  array (
    0 => 
    array (
      'validDate' => 'May 12',
      'period' => 
      array (
        0 => 
        array (
          'validTime' => '00',
          'temperature' => '-999',
          'dewpoint' => '-999',
          'rh' => '-999',
          'skyCover' => '-999',
          'windSpeed' => '-1149',
          'windDirection' => '-999',
          'windGust' => '-999',
          'pop' => '-999',
          'qpf' => '-999.00',
          'snowAmt' => '-999.00',
          'snowLevel' => ' -999',
          'wx' => ' -999 ',
        ),
        1 => 
            ...

The syntax for accessing the data in an array is like this:

1 $res['forecastDay'][int]['validDate'] string
2 $res['forecastDay'][int]['period'][int] string
3 $res['forecastDay'][int]['period'][int]['validTime'] string
3 $res['forecastDay'][int]['period'][int]['temperature'] string
3 $res['forecastDay'][int]['period'][int]['dewpoint'] string
3 $res['forecastDay'][int]['period'][int]['rh'] string
3 $res['forecastDay'][int]['period'][int]['skyCover'] string
3 $res['forecastDay'][int]['period'][int]['windSpeed'] string
3 $res['forecastDay'][int]['period'][int]['windDirection'] string
3 $res['forecastDay'][int]['period'][int]['windGust'] string
3 $res['forecastDay'][int]['period'][int]['pop'] string
3 $res['forecastDay'][int]['period'][int]['qpf'] string
3 $res['forecastDay'][int]['period'][int]['snowAmt'] string
3 $res['forecastDay'][int]['period'][int]['snowLevel'] string
3 $res['forecastDay'][int]['period'][int]['wx'] string
3 $res['forecastDay'][int]['period'][int]['minTemp'] string
3 $res['forecastDay'][int]['period'][int]['maxTemp'] string
3 $res['forecastDay'][int]['period'][int]['fret'] string

Or

$days = $res['forecastDay'];
foreach($days as $day => $data){
  echo $data['validDate'] . "\n";
  foreach($data['period'] as $period => $values){
    if(!is_array($values)){break;}
    echo "  Day: $day, Period: $period\n";
    foreach($values as $key => $value){
      echo "    $key => $value\n";
    }

  }
}
if(!is_array($values)){
  foreach($data['period'] as $key => $value){
    echo "    $key => $value\n";
  }
}

The if(!is_array($values)) had to be added because sometimes the last day has only one period. In that case values is null and the key values are in the $period array.

Which outputs:

May 12
  Day: 0, Period: 0
    validTime => 00
    temperature => -999
    dewpoint => -999
    rh => -999
    skyCover => -999
    windSpeed => -1149
    windDirection => -999
    windGust => -999
    pop => -999
    qpf => -999.00
    snowAmt => -999.00
    snowLevel =>  -999
    wx =>  -999 
  Day: 0, Period: 1
    validTime => 06
    minTemp => 39
    temperature => -999
    dewpoint => -999
    rh => -999
    skyCover => -999
    windSpeed => -1149
    windDirection => -999
    windGust => -999
    pop => -999
    qpf => -999.00
    snowAmt => -999.00
    snowLevel =>  -999
    wx =>  -999 
  Day: 0, Period: 2
    validTime => 12
    temperature => 39
    dewpoint => 25
    rh => 58
    skyCover => 0
    windSpeed => 2
    windDirection => 130
    windGust => 3
    pop => 0
    qpf => 0.00
    snowAmt => 0.00
    snowLevel => 9871
    wx => 
  Day: 0, Period: 3
    validTime => 18
    maxTemp => 76
    temperature => 63
    dewpoint => 27
    rh => 25
    skyCover => 8
    windSpeed => 1
    windDirection => 240
    windGust => 2
    fret => 0.23
    pop => 0
    qpf => 0.00
    snowAmt => 0.00
    snowLevel => 10638
    wx => 
May 13
  Day: 1, Period: 0
    validTime => 00
    temperature => 75
    dewpoint => 21
    rh => 13
    skyCover => 1
    windSpeed => 3
    windDirection => 260
    windGust => 4
    pop => 0
    qpf => 0.00
    snowAmt => 0.00
    snowLevel => 11989
    wx => 
  Day: 1, Period: 1
    validTime => 06
    minTemp => 39
    temperature => 56
    dewpoint => 31
    rh => 39
    skyCover => 2
    windSpeed => 6
    windDirection => 130
    windGust => 6
    pop => 0
    qpf => 0.00
    snowAmt => 0.00
    snowLevel => 12600
    wx => 
  Day: 1, Period: 2
    validTime => 12
    temperature => 48
    dewpoint => 29
    rh => 47
    skyCover => 2
    windSpeed => 5
    windDirection => 160
    windGust => 5
    pop => 0
    qpf => 0.00
    snowAmt => 0.00
    snowLevel => 12393
    wx => 
  Day: 1, Period: 3
    validTime => 18
    maxTemp => 84
    temperature => 74
    dewpoint => 29
    rh => 19
    skyCover => 8
    windSpeed => 3
    windDirection => 180
    windGust => 4
    fret => 0.27
    pop => 1
    qpf => 0.00
    snowAmt => 0.00
    snowLevel => 12678
    wx => 
May 14
  Day: 2, Period: 0
    validTime => 00
    temperature => 82
    dewpoint => 24
    rh => 12
    skyCover => 6
    windSpeed => 8
    windDirection => 230
    windGust => 10
    pop => 0
    qpf => 0.00
    snowAmt => 0.00
    snowLevel => 13551
    wx => 
  Day: 2, Period: 1
    validTime => 06
    minTemp => 48
    temperature => 61
    dewpoint => 35
    rh => 37
    skyCover => 15
    windSpeed => 7
    windDirection => 150
    windGust => 9
    pop => 3
    qpf => 0.00
    snowAmt => 0.00
    snowLevel => 13686
    wx => 
  Day: 2, Period: 2
    validTime => 12
    temperature => 53
    dewpoint => 33
    rh => 46
    skyCover => 16
    windSpeed => 7
    windDirection => 150
    windGust => 9
    pop => 4
    qpf => 0.00
    snowAmt => 0.00
    snowLevel => 13454
    wx => 
  Day: 2, Period: 3
    validTime => 18
    maxTemp => 84
    temperature => 75
    dewpoint => 30
    rh => 19
    skyCover => 33
    windSpeed => 6
    windDirection => 190
    windGust => 6
    fret => 0.31
    pop => 4
    qpf => 0.00
    snowAmt => 0.00
    snowLevel => 12762
    wx => 
May 15
  Day: 3, Period: 0
    validTime => 00
    temperature => 83
    dewpoint => 25
    rh => 12
    skyCover => 34
    windSpeed => 7
    windDirection => 220
    windGust => 9
    pop => 4
    qpf => 0.00
    snowAmt => 0.00
    snowLevel => 13274
    wx => 
  Day: 3, Period: 1
    validTime => 06
    minTemp => 53
    temperature => 62
    dewpoint => 31
    rh => 31
    skyCover => 39
    windSpeed => 8
    windDirection => 170
    windGust => 10
    pop => 7
    qpf => 0.00
    snowAmt => 0.00
    snowLevel => 12170
    wx => 
  Day: 3, Period: 2
    validTime => 12
    temperature => 51
    dewpoint => 31
    rh => 47
    skyCover => 49
    windSpeed => 6
    windDirection => 180
    windGust => 6
    pop => 18
    qpf => 0.00
    snowAmt => 0.00
    snowLevel => 10970
    wx => SChc RW
  Day: 3, Period: 3
    validTime => 18
    maxTemp => 72
    temperature => 69
    dewpoint => 42
    rh => 38
    skyCover => 65
    windSpeed => 10
    windDirection => 230
    windGust => 12
    fret => 0.21
    pop => 43
    qpf => 0.01
    snowAmt => 0.00
    snowLevel => 10422
    wx => Chc T
May 16
  Day: 4, Period: 0
    validTime => 00
    temperature => 69
    dewpoint => 39
    rh => 33
    skyCover => 65
    windSpeed => 8
    windDirection => 260
    windGust => 10
    pop => 43
    qpf => 0.03
    snowAmt => 0.00
    snowLevel => 10503
    wx => Chc T
  Day: 4, Period: 1
    validTime => 06
    minTemp => 51
    temperature => 54
    dewpoint => 40
    rh => 58
    skyCover => 67
    windSpeed => 3
    windDirection => 290
    windGust => 0
    pop => 33
    qpf => 0.00
    snowAmt => 0.00
    snowLevel => 9886
    wx => Chc RW
  Day: 4, Period: 2
    validTime => 12
    temperature => 47
    dewpoint => 38
    rh => 70
    skyCover => 67
    windSpeed => 7
    windDirection => 260
    windGust => 0
    pop => 33
    qpf => 0.00
    snowAmt => 0.00
    snowLevel => 9141
    wx => Chc RW
  Day: 4, Period: 3
    validTime => 18
    maxTemp => 64
    temperature => 60
    dewpoint => 41
    rh => 50
    skyCover => 65
    windSpeed => 8
    windDirection => 300
    windGust => 0
    fret => 0.15
    pop => 34
    qpf => 0.00
    snowAmt => 0.00
    snowLevel => 9387
    wx => Chc RW
May 17
  Day: 5, Period: 0
    validTime => 00
    temperature => 62
    dewpoint => 40
    rh => 45
    skyCover => 65
    windSpeed => 13
    windDirection => 320
    windGust => 0
    pop => 34
    qpf => 0.00
    snowAmt => 0.00
    snowLevel => 9336
    wx => Chc RW
  Day: 5, Period: 1
    validTime => 06
    minTemp => 47
    temperature => 48
    dewpoint => 40
    rh => 73
    skyCover => 59
    windSpeed => 3
    windDirection => 100
    windGust => 0
    pop => 24
    qpf => 0.00
    snowAmt => 0.00
    snowLevel => 9080
    wx => SChc RW
  Day: 5, Period: 2
    validTime => 12
    temperature => 43
    dewpoint => 37
    rh => 79
    skyCover => 59
    windSpeed => 8
    windDirection => 250
    windGust => 0
    pop => 24
    qpf => 0.00
    snowAmt => 0.00
    snowLevel => 8772
    wx => SChc RW
  Day: 5, Period: 3
    validTime => 18
    maxTemp => 68
    temperature => 62
    dewpoint => 40
    rh => 45
    skyCover => 59
    windSpeed => 7
    windDirection => 310
    windGust => 0
    fret => 0.16
    pop => 24
    qpf => 0.00
    snowAmt => 0.00
    snowLevel => 9257
    wx => SChc T
May 18
  Day: 6, Period: 0
    validTime => 00
    temperature => 67
    dewpoint => 37
    rh => 33
    skyCover => 59
    windSpeed => 12
    windDirection => 310
    windGust => 0
    pop => 24
    qpf => 0.00
    snowAmt => 0.00
    snowLevel => 10324
    wx => SChc T
  Day: 6, Period: 1
    validTime => 06
    minTemp => 42
    temperature => 50
    dewpoint => 38
    rh => 62
    skyCover => 60
    windSpeed => 6
    windDirection => 220
    windGust => 0
    pop => 21
    qpf => 0.00
    snowAmt => 0.00
    snowLevel => 9610
    wx => SChc RW
  Day: 6, Period: 2
    validTime => 12
    temperature => 44
    dewpoint => 35
    rh => 71
    skyCover => 60
    windSpeed => 6
    windDirection => 250
    windGust => 0
    pop => 21
    qpf => 0.00
    snowAmt => 0.00
    snowLevel => 8696
    wx => SChc RW
  Day: 6, Period: 3
    validTime => 18
    maxTemp => 68
    temperature => 61
    dewpoint => 35
    rh => 37
    skyCover => 49
    windSpeed => 8
    windDirection => 310
    windGust => 0
    fret => 0.18
    pop => 15
    qpf => 0.00
    snowAmt => 0.00
    snowLevel => 8661
    wx => SChc T
May 19
    validTime => 00
    temperature => 67
    dewpoint => 31
    rh => 26
    skyCover => 49
    windSpeed => 10
    windDirection => 330
    windGust => 0
    pop => 15
    qpf => 0.00
    snowAmt => 0.00
    snowLevel => 9163
    wx => SChc T
Sign up to request clarification or add additional context in comments.

Comments

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.