前兩個註釋行失敗,被禁止的錯誤
諾阿從file_get_contents
和simplexml_load_file()
返回的請求403禁止HTTP狀態但Firefox也拿不就好了。
所以我創建了一個看起來就像火狐捲曲請求。
然後我轉換的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))
必須添加,因爲有時最後一天只有一個時期。在這種情況下,值爲空,鍵值在$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
我已經嘗試過了,似乎工作。唯一的問題是XML包含一個長度爲4790個字符的項目'textContent'。我錯過了這一點,或者你想達到什麼目標? – Richard
每當我嘗試這個我的'textContent'總是空的。 我需要從這個XML文件中提取沉澱和相對溼度。 我有點新的PHP,所以如果你有任何其他的方式,這可能會更好地工作,即時通訊完全開放 – Skylit
增加了一個foreach循環來查看所有的數據。 – Misunderstood