2016-05-12 9 views
2

私はオーバーフローするのが新しいですが、私は失われてしまいました。そして、私が使用して$XMLをプリントアウトしてい特定のXML URLをPHPで解析する

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

:このPHPコードを使用してhttp://www.wrh.noaa.gov/forecast/xml/xml.php?duration=168&interval=6&lat=39.9233&lon=-111.883 :私はこのXMLを解析しようとしてる

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

私は他のURLにこのコードを使用して、それいつも働いていたので、なぜこのURLでうまく動作しないのかわかりません。 フィードバックをいただければ幸いです。

+0

をして動作するようです:出力

。問題は、XMLに4790文字のテキスト 'textContent'という項目が含まれていることだけです。私はポイントを逃している、またはあなたが達成したい目標は何ですか? – Richard

+0

私はこれを試すときはいつも私の 'textContent'は常に空です。 このXMLファイルから降水量と相対湿度を引き出す必要があります。 私はちょっとPHPに少し新しくなっています。あなたが他の方法をとっていればもっとうまくいくかもしれません。 – Skylit

+0

すべてのデータを見るためにforeachループが追加されました。 – Misunderstood

答えて

3

最初の2行をコメントが禁止のエラーで失敗しました

NOAAはfile_get_contentssimplexml_load_file()からご要望に応じて403禁止されたHTTPステータスを返されたが、Firefoxはそれがうまく得ることができます。

私は、FireFoxのように見えるカールリクエストを作成しました。

次に、XMLを配列に変換しました。私はオブジェクトではなく配列で作業するほうが好きです。

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)):配列内のデータにアクセスするための構文は次のようである

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


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

これは、これを与えました場合によっては最後の日に1つの期間しかないために追加する必要がありました。その場合、値は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