Wyciąganie danych ze stringa


(northwest) #1

Witam serdecznie,

Mam następujący skrypt:

 

$zipcode = 'UKXX0014';

$result = file_get_contents('http://weather.yahooapis.com/forecastrss?p=' . $zipcode . '&u=c');
$xml = new DOMDocument();
$xml->loadXML($result);
$xpath = new DOMXPath($xml);
 
echo htmlspecialchars($result, ENT_QUOTES, 'UTF-8');
 
$location = $xpath->query('channel/yweather:location')->item(0);
 
if(!empty($location)){
    $items = $xpath->query('channel/item');
    foreach($items as $item){
        $current = $xpath->query('yweather:condition', $item)->item(0);
        $forecast = $xpath->query('yweather:forecast', $item);
        $output = <<<END
            <h1 style="margin-bottom: 0">Weather for {$location->getAttribute('city')}, {$location->getAttribute('region')}</h1>
            <small>{$current->getAttribute('date')}</small>
            <h2>Current Conditions</h2>
            <p>
            <span style="font-size:72px; font-weight:bold;">{$current->getAttribute('temp')}&deg;F</span>
            <br/>
            <img src="http://l.yimg.com/a/i/us/we/52/{$current->getAttribute('code')}.gif" style="vertical-align: middle;"/>&nbsp;
            {$current->getAttribute('text')}
            </p>
            <h2>Forecast</h2>
            {$forecast->item(0)->getAttribute('day')} - {$forecast->item(0)->getAttribute('text')}. High: {$forecast->item(0)->getAttribute('high')} Low: {$forecast->item(0)->getAttribute('low')}
            <br/>
            {$forecast->item(1)->getAttribute('day')} - {$forecast->item(1)->getAttribute('text')}. High: {$forecast->item(1)->getAttribute('high')} Low: {$forecast->item(1)->getAttribute('low')}
            </p>
END;
}

Zwraca mi następujące dane:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
<channel>

<title>Yahoo! Weather - Bedford, UK</title>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Bedford__UK/*http://weather.yahoo.com/forecast/UKXX0014_c.html</link>
<description>Yahoo! Weather for Bedford, UK</description>
<language>en-us</language>
<lastBuildDate>Wed, 02 Jul 2014 6:19 pm BST</lastBuildDate>
<ttl>60</ttl>
<yweather:location city="Bedford" region="" country="UK"/>
<yweather:units temperature="C" distance="km" pressure="mb" speed="km/h"/>
<yweather:wind chill="21" direction="230" speed="11.27" />
<yweather:atmosphere humidity="53" visibility="9.99" pressure="1015.92" rising="0" />
<yweather:astronomy sunrise="4:44 am" sunset="9:25 pm"/>
<image>
<title>Yahoo! Weather</title>
<width>142</width>
<height>18</height>
<link>http://weather.yahoo.com</link>
<url>http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif</url>
</image>
<item>
<title>Conditions for Bedford, UK at 6:19 pm BST</title>
<geo:lat>52.13</geo:lat>
<geo:long>-.46</geo:long>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Bedford__UK/*http://weather.yahoo.com/forecast/UKXX0014_c.html</link>
<pubDate>Wed, 02 Jul 2014 6:19 pm BST</pubDate>
<yweather:condition text="Partly Cloudy" code="30" temp="21" date="Wed, 02 Jul 2014 6:19 pm BST" />
<description><![CDATA[
<img src="http://l.yimg.com/a/i/us/we/52/30.gif"/><br />
<b>Current Conditions:</b><br />
Partly Cloudy, 21 C<BR />
<BR /><b>Forecast:</b><BR />
Wed - Partly Cloudy. High: 23 Low: 12<br />
Thu - Sunny. High: 26 Low: 13<br />
Fri - Partly Cloudy. High: 27 Low: 14<br />
Sat - Showers. High: 22 Low: 11<br />
Sun - Scattered Showers. High: 21 Low: 9<br />
<br />
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Bedford__UK/*http://weather.yahoo.com/forecast/UKXX0014_c.html">Full Forecast at Yahoo! Weather</a><BR/><BR/>
(provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>
]]></description>
<yweather:forecast day="Wed" date="2 Jul 2014" low="12" high="23" text="Partly Cloudy" code="29" />
<yweather:forecast day="Thu" date="3 Jul 2014" low="13" high="26" text="Sunny" code="32" />
<yweather:forecast day="Fri" date="4 Jul 2014" low="14" high="27" text="Partly Cloudy" code="30" />
<yweather:forecast day="Sat" date="5 Jul 2014" low="11" high="22" text="Showers" code="11" />
<yweather:forecast day="Sun" date="6 Jul 2014" low="9" high="21" text="Scattered Showers" code="39" />
<guid isPermaLink="false">UKXX0014_2014_07_06_7_00_BST</guid>
</item>
</channel>
</rss>

<!-- api8.weather.bf1.yahoo.com Wed Jul 2 15:59:52 PDT 2014 -->

 

 

chciałbym wyciągnąć z powyższego tekstu:

$dzisiajmin - dzisiejsza temperatura minimalna, $dzisiajmax - dzisiejsza temperatura maksymalna

$dzien1min - jutrzejsza temperatura minimalna, $dzien1max - jutrzejsza temperatura maksymalna

$dzien2min - temperatura minimalna za 2 dni, $dzien2max - temperatura maksymalna za 2 dni

i tak do 5 dnia :slight_smile:

 

Wie ktoś może jak to zrobić?

 

Bardzo proszę o pomoc,

Łukasz


(StawikPiast) #2

Przecież to jest XML. Załaduj to jako XML i wyciągaj dane.


(Kamill1985) #3

Dokładnie, skorzystaj z parsera xmla.