parsing XML from weather.com

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

parsing XML from weather.com

Post by irishmike2004 »

I am extremely new at PHP and relatively new to OO Programming as well. The code that follows confuses me because I still don't quite have a grip on "this" in coding.

Code: Select all

$xmlparser = xml_parser_create();
			if (!xml_parse_into_struct($xmlparser, $this->_xml, $this->_values, $this->_indexes)) {
				$this->_error = "Unparsable XML";
				return(0);
			}
	
			xml_parser_free($xmlparser);
I need to take some XML I got from weather channel via the curl command and get data from it. The data would I want would be: Current Temp, Condition, Wind, and Feels Like... these would go into their various variables.

Here is the output from the curl command:

en_US
MEDIUM
F mi mph in in Shawnee Mission, KS (66214) 11:45 AM 38.96 -94.71 7:21 AM 4:57 PM -6 http://www.weather.com/outlook/health/a ... 4?par=xoap Pollen Reports http://www.weather.com/outlook/travel/f ... 4?par=xoap Airport Delays http://www.weather.com/outlook/events/s ... k&par=xoap Special Events http://www.weather.com/services/desktop.html?par=xoap Download Desktop Weather 12/3/04 10:53 AM CST Olathe Executive, KS 41 36 Fair 34 30.06 falling 8 N/A 200 SSW 67 10.0 2 Low 31 20 Waning Gibbous 12/3/04 11:03 AM CST 44 32 7:21 AM 4:57 PM 30 Partly Cloudy 10 N/A 248 WSW 0 63 31 Clear 7 N/A 234 SW 0 81 52 42 7:22 AM 4:56 PM 32 Sunny 12 N/A 206 SSW 0 68 29 Partly Cloudy 11 N/A 181 S 0 77 54 40 7:23 AM 4:56 PM 11 Showers 13 N/A 172 S 60 81 29 Clouds Early / Clearing Late 7 N/A 197 SSW 10 85 51 28 7:24 AM 4:56 PM 30 Partly Cloudy 13 N/A 261 W 20 74 29 Partly Cloudy 10 N/A 313 NW 20 64

End output

the actual XML looks like this:


Code: Select all

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--This document is intended only for use by authorized licensees of The Weather Channel. Unauthorized use is prohibited. Copyright 1995-2004, The Weather Channel Enterprises, Inc. All Rights Reserved.-->
<weather ver="2.0">
  <head>
    <locale>en_US</locale>
    <form>MEDIUM</form>
    <ut>F</ut>
    <ud>mi</ud>
    <us>mph</us>
    <up>in</up>
    <ur>in</ur>
  </head>
  <loc id="66214">
    <dnam>Shawnee Mission, KS (66214)</dnam>
    <tm>12:00 PM</tm>
    <lat>38.96</lat>
    <lon>-94.71</lon>
    <sunr>7:21 AM</sunr>
    <suns>4:57 PM</suns>
    <zone>-6</zone>
  </loc>
  <lnks type="prmo">
    <link pos="1">
      <l>http://www.weather.com/outlook/health/allergies/66214?par=xoap</l>
      <t>Pollen Reports</t>
    </link>
    <link pos="2">
      <l>http://www.weather.com/outlook/travel/flights/citywx/66214?par=xoap</l>
      <t>Airport Delays</t>
    </link>
    <link pos="3">
      <l>http://www.weather.com/outlook/events/special/result/66214?when=thisweek&par=xoap</l>
      <t>Special Events</t>
    </link>
    <link pos="4">
      <l>http://www.weather.com/services/desktop.html?par=xoap</l>
      <t>Download Desktop Weather</t>
    </link>
  </lnks>
  <cc>
    <lsup>12/3/04 11:53 AM CST</lsup>
    <obst>Olathe Executive, KS</obst>
    <tmp>42</tmp>
    <flik>36</flik>
    <t>Fair</t>
    <icon>34</icon>
    <bar>
      <r>30.04</r>
      <d>falling</d>
    </bar>
    <wind>
      <s>10</s>
      <gust>N/A</gust>
      <d>230</d>
      <t>SW</t>
    </wind>
    <hmid>62</hmid>
    <vis>10.0</vis>
    <uv>
      <i>2</i>
      <t>Low</t>
    </uv>
    <dewp>30</dewp>
    <moon>
      <icon>20</icon>
      <t>Waning Gibbous</t>
    </moon>
  </cc>
  <dayf>
    <lsup>12/3/04 11:03 AM CST</lsup>
    <day d="0" t="Friday" dt="Dec 3">
      <hi>44</hi>
      <low>32</low>
      <sunr>7:21 AM</sunr>
      <suns>4:57 PM</suns>
      <part p="d">
        <icon>30</icon>
        <t>Partly Cloudy</t>
        <wind>
          <s>10</s>
          <gust>N/A</gust>
          <d>248</d>
          <t>WSW</t>
        </wind>
        <ppcp>0</ppcp>
        <hmid>63</hmid>
      </part>
      <part p="n">
        <icon>31</icon>
        <t>Clear</t>
        <wind>
          <s>7</s>
          <gust>N/A</gust>
          <d>234</d>
          <t>SW</t>
        </wind>
        <ppcp>0</ppcp>
        <hmid>81</hmid>
      </part>
    </day>
    <day d="1" t="Saturday" dt="Dec 4">
      <hi>52</hi>
      <low>42</low>
      <sunr>7:22 AM</sunr>
      <suns>4:56 PM</suns>
      <part p="d">
        <icon>32</icon>
        <t>Sunny</t>
        <wind>
          <s>12</s>
          <gust>N/A</gust>
          <d>206</d>
          <t>SSW</t>
        </wind>
        <ppcp>0</ppcp>
        <hmid>68</hmid>
      </part>
      <part p="n">
        <icon>29</icon>
        <t>Partly Cloudy</t>
        <wind>
          <s>11</s>
          <gust>N/A</gust>
          <d>181</d>
          <t>S</t>
        </wind>
        <ppcp>0</ppcp>
        <hmid>77</hmid>
      </part>
    </day>
    <day d="2" t="Sunday" dt="Dec 5">
      <hi>54</hi>
      <low>40</low>
      <sunr>7:23 AM</sunr>
      <suns>4:56 PM</suns>
      <part p="d">
        <icon>11</icon>
        <t>Showers</t>
        <wind>
          <s>13</s>
          <gust>N/A</gust>
          <d>172</d>
          <t>S</t>
        </wind>
        <ppcp>60</ppcp>
        <hmid>81</hmid>
      </part>
      <part p="n">
        <icon>29</icon>
        <t>Clouds Early / Clearing Late</t>
        <wind>
          <s>7</s>
          <gust>N/A</gust>
          <d>197</d>
          <t>SSW</t>
        </wind>
        <ppcp>10</ppcp>
        <hmid>85</hmid>
      </part>
    </day>
    <day d="3" t="Monday" dt="Dec 6">
      <hi>51</hi>
      <low>28</low>
      <sunr>7:24 AM</sunr>
      <suns>4:56 PM</suns>
      <part p="d">
        <icon>30</icon>
        <t>Partly Cloudy</t>
        <wind>
          <s>13</s>
          <gust>N/A</gust>
          <d>261</d>
          <t>W</t>
        </wind>
        <ppcp>20</ppcp>
        <hmid>74</hmid>
      </part>
      <part p="n">
        <icon>29</icon>
        <t>Partly Cloudy</t>
        <wind>
          <s>10</s>
          <gust>N/A</gust>
          <d>313</d>
          <t>NW</t>
        </wind>
        <ppcp>20</ppcp>
        <hmid>64</hmid>
      </part>
    </day>
  </dayf>
</weather>
It doesn't appear to keep the format when using shell_exec, it is almost like it parsed it already, (see output above) any suggestion on how this could be better handled would be good.

I want to get at the XML and parse it and it is at a URL which is:

http://xoap.weather.com/weather/local/6 ... Key&unit=s

You need to get a license key and partner id to run this, but I thought it would help to see the URL that fetches the XML (obviously I took my info out for security reasons :-))

Thanks for any help in advance.
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Post by irishmike2004 »

the shell_exec() function definately messes up the XML output. I have modified my URL to get less XML because I am just wanting the current weather so I took off the &dayf=4 to get just "today's weather" which cuts the data amount down by quite a bit.

cc or "current conditions" are what we want from the XML. I was looking at fputs but can't seem to get the data from the site to output so I can verify that we are actually getting the XML. I still need help setting up the parser too though!
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

i prefer to use http://www.php.net/file_get_contents

and then i parse that ;) a very simple example:

http://home.mysth.be/~timvw/programming/php/service.txt
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Tried to follow Tim's example

Post by irishmike2004 »

So I get the same output from the following code as from my curl system call but I like the fact that it works from inside PHP better.

Code: Select all

$data = file_get_contents('http://xoap.weather.com/weather/local/66214?cc=*&prod=xoap&link=xoap&par=1006654722&key=5f59a6ef6a90bd7e&unit=s');

// ignore the document type
$data = preg_replace("/\<\?xml.*?\>/", '', $data);
$data = trim($data);
I am still at a loss of how to set up my XML parser to parse the data, the XML we are getting is:

Code: Select all

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--This document is intended only for use by authorized licensees of The Weather Channel. Unauthorized use is prohibited. Copyright 1995-2004, The Weather Channel Enterprises, Inc. All Rights Reserved.-->
<weather ver="2.0">
  <head>
    <locale>en_US</locale>
    <form>MEDIUM</form>
    <ut>F</ut>
    <ud>mi</ud>
    <us>mph</us>
    <up>in</up>
    <ur>in</ur>
  </head>
  <loc id="66214">
    <dnam>Shawnee Mission, KS (66214)</dnam>
    <tm>1:56 PM</tm>
    <lat>38.96</lat>
    <lon>-94.71</lon>
    <sunr>7:21 AM</sunr>
    <suns>4:57 PM</suns>
    <zone>-6</zone>
  </loc>
  <lnks type="prmo">
    <link pos="1">
      <l>http://www.weather.com/outlook/health/allergies/66214?par=xoap</l>
      <t>Pollen Reports</t>
    </link>
    <link pos="2">
      <l>http://www.weather.com/outlook/travel/flights/citywx/66214?par=xoap</l>
      <t>Airport Delays</t>
    </link>
    <link pos="3">
      <l>http://www.weather.com/outlook/events/special/result/66214?when=thisweek&par=xoap</l>
      <t>Special Events</t>
    </link>
    <link pos="4">
      <l>http://www.weather.com/services/desktop.html?par=xoap</l>
      <t>Download Desktop Weather</t>
    </link>
  </lnks>
  <cc>
    <lsup>12/3/04 12:53 PM CST</lsup>
    <obst>Olathe Executive, KS</obst>
    <tmp>46</tmp>
    <flik>40</flik>
    <t>Fair</t>
    <icon>34</icon>
    <bar>
      <r>30.01</r>
      <d>falling</d>
    </bar>
    <wind>
      <s>14</s>
      <gust>N/A</gust>
      <d>230</d>
      <t>SW</t>
    </wind>
    <hmid>47</hmid>
    <vis>10.0</vis>
    <uv>
      <i>2</i>
      <t>Low</t>
    </uv>
    <dewp>27</dewp>
    <moon>
      <icon>21</icon>
      <t>Waning Gibbous</t>
    </moon>
  </cc>
</weather>
of which I want data just in between the <cc></cc> tags. The specific data I am after is the data between the following tags:

<tmp><tmp>, <flik></flik>, <t></t>, <icon></icon>, <bar.</bar>. and <wind></wind>

Obviously the <wind> and <bar> tag have sub-tags.

SO the question is:

How do I set up my parser to get that data from the XML above.

Thanks... I am 3 years old here, so please explain clearly and simply.

BTW: I want to put each of those into a variable like this:

$temp = <tmp>
$feelsLike = <flik>
$wIcon = <icon>
$condition = <t>
$windSpeed = <wind><s>
$windDir = <wind><t>
$pressure = <bar><r>
$barometer = <bar><d>

Hopefully someone can help me set this up to parse correctly.

thanks,
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Post by irishmike2004 »

be nice to have <vis> and <uv> parsed as well :-)
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Starting new Thread

Post by irishmike2004 »

Hi All:

Because I have been reading the docs and looked at the example provided by Timvw, I have a better understanding of what is happening when I try to parse my XML from weather.com, however, I have developed some code and feel that a new thread is in order... this one is getting too long to rehash what I have figured out!
Post Reply