$result = file_get_contents('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22Sarajevo%22)&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys');
$xml = simplexml_load_string($result);
//echo htmlspecialchars($result, ENT_QUOTES, 'UTF-8');
$xml->registerXPathNamespace('yweather', 'http://xml.weather.yahoo.com/ns/rss/1.0');
$weather_location = $xml->channel->xpath('yweather:location');
if(!empty($weather_location)){
foreach($xml->channel->item as $item){
$forecast = $item->xpath('yweather:forecast');
$time = time();
$wdate = strtotime($forecast[$i]['day']);
echo "".(string)$wdate.""; // Wed
echo "<span><b>".(int)$forecast[$i]['high']."°</b>";// 8
echo " ".(int)$forecast[$i]['low']."°</span>"; // 19
}
}
サンプルXML:
警告:にSimpleXMLElement :: XPathの()[のSimpleXMLElementこれが結果です
<?xml version="1.0" encoding="UTF-8"?> <query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="1" yahoo:created="2016-03-31T10:56:53Z" yahoo:lang="en-US"> <results> <channel> <yweather:units xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" distance="mi" pressure="in" speed="mph" temperature="F"/> <title>Yahoo! Weather - Sarajevo, Federacija Bosne I Hercegovine, BA</title> <link>http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-943828/</link> <description>Yahoo! Weather for Sarajevo, Federacija Bosne I Hercegovine, BA</description> <language>en-us</language> <lastBuildDate>Thu, 31 Mar 2016 12:56 PM CEST</lastBuildDate> <ttl>60</ttl> <yweather:location xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" city="Sarajevo" country="Bosnia and Herzegovina" region=" Federacija Bosne I Hercegovine"/> <yweather:wind xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" chill="70" direction="270" speed="4"/> <yweather:atmosphere xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" humidity="43" pressure="922.0" rising="0" visibility="16.1"/> <yweather:astronomy xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" sunrise="6:27 am" sunset="7:14 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 Sarajevo, Federacija Bosne I Hercegovine, BA at 12:00 PM CEST</title> <geo:lat xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">43.853161</geo:lat> <geo:long xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">18.38493</geo:long> <link>http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-943828/</link> <pubDate>Thu, 31 Mar 2016 12:00 PM CEST</pubDate> <yweather:condition xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="32" date="Thu, 31 Mar 2016 12:00 PM CEST" temp="69" text="Sunny"/> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="30" date="31 Mar 2016" day="Thu" high="70" low="41" text="Partly Cloudy"/> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="28" date="01 Apr 2016" day="Fri" high="72" low="50" text="Mostly Cloudy"/> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="30" date="02 Apr 2016" day="Sat" high="68" low="44" text="Partly Cloudy"/> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="30" date="03 Apr 2016" day="Sun" high="69" low="45" text="Partly Cloudy"/> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="30" date="04 Apr 2016" day="Mon" high="69" low="47" text="Partly Cloudy"/> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="30" date="05 Apr 2016" day="Tue" high="69" low="47" text="Partly Cloudy"/> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="30" date="06 Apr 2016" day="Wed" high="69" low="47" text="Partly Cloudy"/> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="28" date="07 Apr 2016" day="Thu" high="66" low="49" text="Mostly Cloudy"/> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="28" date="08 Apr 2016" day="Fri" high="58" low="50" text="Mostly Cloudy"/> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="28" date="09 Apr 2016" day="Sat" high="58" low="47" text="Mostly Cloudy"/> <description><![CDATA[<img src="http://l.yimg.com/a/i/us/we/52/32.gif"/> <BR /> <b>Current Conditions:</b> <BR />Sunny <BR /> <BR /> <b>Forecast:</b> <BR /> Thu - Partly Cloudy. High: 70Low: 41 <BR /> Fri - Mostly Cloudy. High: 72Low: 50 <BR /> Sat - Partly Cloudy. High: 68Low: 44 <BR /> Sun - Partly Cloudy. High: 69Low: 45 <BR /> Mon - Partly Cloudy. High: 69Low: 47 <BR /> <BR /> <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-943828/">Full Forecast at Yahoo! Weather</a> <BR /> <BR /> (provided by <a href="http://www.weather.com" >The Weather Channel</a>) <BR /> ]]></description> <guid isPermaLink="false"/> </item> </channel> </results> </query>
.xpath]:/home/ugbosnia/public_html/blocks/weather/block.php 8行目の未定義の名前空間接頭辞
警告:にSimpleXMLElement :: XPathの()[simplexmlelement.xpath]:xmlXPathEval:評価はライン8
に/home/ugbosnia/public_html/blocks/weather/block.phpで任意の提案を失敗しましたか?
実際には、XMLは非常に簡単に処理することができますXパス。 @ Ahmed-Gharamtiご質問に小さなサンプルXMLを追加してください。私がURLを呼び出すと、あなたのソースと一致しない空のXMLがほとんど得られます。 – ThW
サンプルXMLを追加しました –