How to get the tag yweather:condition from Yahoo Weather RSS in PHP?
Something like:
echo $itemgotten->getElementsByTagNameNS(
"http://xml.weather.yahoo.com/ns/rss/1.0","condition")->item(0)->
getAttribute("temp");
The key is that you have to use getElementsByTagNameNS
instead of getElementsByTagName
and specify "http://xml.weather.yahoo.com/ns/rss/1.0"
as the namespace.
You know yweather
is a shortcut for http://xml.weather.yahoo.com/ns/rss/1.0
because the XML file includes a xmls
attribute:
<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#">
yahoo weather Api
This seems pretty straightforward. From the tutorial:
Since the only bit of information we care about is the
yweather:condition
element'stext
attribute, we're going to avoid creating an XML parsing object and use a short regular expression.
So, just look at the line with the regular expression:
$weather_class = format_result(
get_match( '/<yweather:condition text="(.*)"/isU', $data )
);
This is actually a bad regular expression because it assumes text
will always be the first attribute (and that there'll always be that weird double-space. Here's a regular expression that will get the temp
attribute regardless of where it falls:
/<yweather:condition\s+(?:.*\s)?temp="(.+)"/isU
Substitute that for the regular expression given to get_match()
and you should be good to go.
Oh, and lest I be kicked off SO for not saying so: Attempting to parse arbitrary HTML XML with regular expressions is the path to madness.
Yahoo Weather API: Get data from another day
It doesn't look like the Yahoo API supports extended forecasts. Take a look at the Weather Underground API, they have a 10 day forecast available.
Yahoo Weather Rss Reading with ASP.NET MVC?
The following expression should work, first reference the ycweather
namespace;
XNamespace yWeatherNS = "http://xml.weather.yahoo.com/ns/rss/1.0";
then you get the attribute values this way:
Text = feed.Element(yWeatherNS + "condition").Attribute("text").Value
The problem is that your condition element in in another namespace, so you must select this node in the context of that namespace via the XNamespace.
You can read more about XML Namespaces via the MSDN article Namespaces in C#
PHP and simplexml_load_file()
There is a very good info at the said link..
Related Topics
How to Start a Windows Gui Program Using PHP
Set Woocommerce Cart Expiration
Php: What Is the Purpose of Session_Name
Using PHP Replace Regex with Regex
What Is the Use of Strict in Laravel Config/Database
How to Create a Nested Array Out of an Array in PHP
Php's Simplexml: How to Use Colons in Names
$_Server['Remote_Port'] Not Giving Value
How to Get Woocommerce Variation Id
Display Price on Add to Cart Button from the Functions.PHP File in Woocommerce
How to Get Average of Column Values in Laravel
Wordpress Remove Robots Meta Tag Noindex
Set the Option Available for Select Based on Another Select PHP
Get the Auto-Generated Id After an Insert
What Does an Exclamation Mark Before a Function Really Mean in PHP