php - 在 php 中解析特定的 XML URL

标签 php xml url

我是堆栈溢出的新手,但我对这个问题感到非常迷失和慌乱,我只需要问一下。我正在尝试解析此 XML:http://www.wrh.noaa.gov/forecast/xml/xml.php?duration=168&interval=6&lat=39.9233&lon=-111.883 使用这个 php 代码:

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

然后我使用以下方法打印 $XML:

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

我已将此代码与其他 URL 一起使用,并且它始终有效,所以我不明白为什么它不适用于此特定 URL 我真的很感激一些反馈。

最佳答案

前两行注释因禁止错误而失败

noaa 对来自 file_get_contentssimplexml_load_file() 的请求返回了 403 禁止的 http 状态,但 FireFox 可以很好地获取它。

所以我创建了一个看起来像 FireFox 的curl 请求。

然后我将 XML 转换为数组。我更喜欢使用数组而不是对象。

<?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;
  }
}
?>

这给出了这个:

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 => 
            ...

访问数组中数据的语法如下:

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

或者

$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";
  }
}

必须添加 if(!is_array($values)) 因为有时最后一天只有一个句点。在这种情况下,值将为 null,键值位于 $period 数组中。

哪些输出:

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

关于php - 在 php 中解析特定的 XML URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37193994/

相关文章:

php - MYSQL 仅从表中显示一次列值

JavaScript XML 搜索 - 搜索时显示错误消息

xml - OSB 中命名空间修改的 XSLT - 命名空间前缀奇怪?

Java获取具有正确编码的url

javascript - Jquery ui 选项卡仅在单击时加载 iframe

javascript - 获取列表项的值并POST到ajax

php - 如何对包含正斜杠的查询字符串进行编码?

java - 更改struts 2中的显示URL以隐藏请求参数

PHP:通过核心类/函数强制使用不同命名空间中的扩展类

java - 如何创建简单的 WSDL?