Your SlideShare is downloading. ×
SimpleXML In PHP 5
SimpleXML In PHP 5
SimpleXML In PHP 5
SimpleXML In PHP 5
SimpleXML In PHP 5
SimpleXML In PHP 5
SimpleXML In PHP 5
SimpleXML In PHP 5
SimpleXML In PHP 5
SimpleXML In PHP 5
SimpleXML In PHP 5
SimpleXML In PHP 5
SimpleXML In PHP 5
SimpleXML In PHP 5
SimpleXML In PHP 5
SimpleXML In PHP 5
SimpleXML In PHP 5
SimpleXML In PHP 5
SimpleXML In PHP 5
SimpleXML In PHP 5
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

SimpleXML In PHP 5

4,210

Published on

Demonstrating SimpleXML in PHP5 by utilizing it to grab an XML weather feed from the National Weather Service and display the data, marked up via CSS, on a live site.

Demonstrating SimpleXML in PHP5 by utilizing it to grab an XML weather feed from the National Weather Service and display the data, marked up via CSS, on a live site.

Published in: Technology, Design
6 Comments
2 Likes
Statistics
Notes
No Downloads
Views
Total Views
4,210
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
78
Comments
6
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. SimpleXML Presented by Ron Pringle Webmaster City of Aurora, Illinois
  • 2. What is XML?
    • XML stands for EXtensible Markup Language
    • XML is a markup language much like HTML
    • XML was designed to carry data, not to display data
    • XML tags are not predefined. You must define your own tags
    • XML is designed to be self-descriptive
    • XML is a W3C Recommendation
    Ex.1 Sample XML data
  • 3. What is SimpleXML?
    • SimpleXML is a PHP extension that converts an XML document into an object that can then be processed with normal property selectors and array iterators.
    • SimpleXML functions are part of the PHP core. No installation required.
    • SimpleXML requires PHP5+ and is enabled by default.
  • 4. Example 1: Loading an RSS Feed
    • Our sample XML file structure can be seen in ex.2
    • Create new simpleXML object by loading the file with simplexml_load_file function.
    Ex.2 Sample RSS feed in XML format $xml = simplexml_load_file(‘sample.xml’);
  • 5. RSS Feed Code
    • 01: <?php
    • 02: // Create new simpleXML object
    • 03: $xml = simplexml_load_file(‘sample.xml’);
    • 04: //Access the title element
    • 05: echo “<h2>” . $xml->channel->title . “</h2> ”;
    • 06: //Access all titles, descriptions, links in array
    • 07: foreach($xml->item as $item) {
    • 08: echo “<h3>” . $item->title . “</h3> ”;
    • 09: echo “<p> . $item->description . “</p> ”;
    • 10: echo “<p><a href=”” . $item->link . “”>More Info</a></p> ”;
    • 11: } //end foreach xml item
    • 12: ?>
  • 6. RSS Feed Display Ex.3 Output from code in example 1
  • 7. RSS Feed Display
    • Channel title
    Ex.4 Output from code in example 1 with explanations Item title Item description Item link
  • 8. Access Single Item in Array Ex.4 XML data echo “<h3>” . $xml->item[0]->title . “</h3> ”; will return the title for the first item in the array, item 0, which is: City Offers Activities to Celebrate National Historic Preservation Month A single item in the array can be accessed by referring to its position. The following code
  • 9. Example 2: NWS Weather Feed
    • National Weather Service has XML feeds for about 1,800 locations across the U.S. and U.S. Territories.
    • Why use this when there are all those free weather widgets out there?
    • This method allows us to control the data, what we want to display, the look and feel of the display, and eliminates commercial advertising from our site.
  • 10. NWS Weather Feed
    • Locate URL for feed. Aurora Municipal Airport (KARR) http://www.weather.gov/data/ current_obs/KARR.xml
    • View raw XML to figure out what data is there and what the namespaces are
    • Load remote XML file
    • Parse the data we’re interested in displaying
  • 11. Raw XML
    • <current_observation version=&quot;1.0&quot; xsi:noNamespaceSchemaLocation=&quot;http://www.weather.gov/data/current_obs/current_observation.xsd&quot;>
    • <credit>NOAA's National Weather Service</credit>
    • <credit_URL>http://weather.gov/</credit_URL>
    • <image>
    • <url>http://weather.gov/images/xml_logo.gif</url>
    • <title>NOAA's National Weather Service</title>
    • <link>http://weather.gov</link>
    • </image>
    • <suggested_pickup>15 minutes after the hour</suggested_pickup>
    • <suggested_pickup_period>60</suggested_pickup_period>
    • <location>Aurora Municipal Airport, IL</location>
    • <station_id>KARR</station_id>
    • <latitude>41.77</latitude>
    • <longitude>-88.47</longitude>
    • <observation_time>Last Updated on Jun 3, 1:52 pm CDT</observation_time>
    • <observation_time_rfc822>Tue, 3 Jun 2008 13:52:00 -0500 CDT</observation_time_rfc822>
    • <weather>Fog/Mist</weather>
    • <temperature_string>67 F (19 C)</temperature_string>
    • <temp_f>67</temp_f>
    • <temp_c>19</temp_c>
    • <relative_humidity>93</relative_humidity>
    • <wind_string>From the Southeast at 17 MPH</wind_string>
    • <wind_dir>Southeast</wind_dir>
    • <wind_degrees>140</wind_degrees>
    • <wind_mph>17.25</wind_mph>
    • <wind_gust_mph>NA</wind_gust_mph>
    • <pressure_string>29.57&quot; (1001.0 mb)</pressure_string>
    • <pressure_mb>1001.0</pressure_mb>
    • <pressure_in>29.57</pressure_in>
    • <dewpoint_string>65 F (18 C)</dewpoint_string>
    • <dewpoint_f>65</dewpoint_f>
    • <dewpoint_c>18</dewpoint_c>
    • <heat_index_string>NA</heat_index_string>
    • <heat_index_f>NA</heat_index_f>
    • <heat_index_c>NA</heat_index_c>
    • <windchill_string>NA</windchill_string>
    • <windchill_f>NA</windchill_f>
    • <windchill_c>NA</windchill_c>
    • <visibility_mi>5.00</visibility_mi>
    • <icon_url_base>http://weather.gov/weather/images/fcicons/</icon_url_base>
    • <icon_url_name>fg.jpg</icon_url_name>
    • <two_day_history_url>http://www.weather.gov/data/obhistory/KARR.html</two_day_history_url>
    • <ob_url>http://www.nws.noaa.gov/data/METAR/KARR.1.txt</ob_url>
    • <disclaimer_url>http://weather.gov/disclaimer.html</disclaimer_url>
    • <copyright_url>http://weather.gov/disclaimer.html</copyright_url>
    • <privacy_policy_url>http://weather.gov/notice.html</privacy_policy_url>
    • </current_observation>
  • 12. Weather Code
    • 01: $weatherURL = ‘http://www.nws.noaa.gov/data/current_obs/KARR.xml’;
    • 02: if(file_exists($weatherURL)) {
    • 03: $xml = simplexml_load_string($weatherURL);
    • 04: echo ‘<h3 class=“centertxt”>Current Weather</h3>’;
    • 05: echo ‘<img class=“floatleftclearsmallborder” src=“$xml->icon_url_base . $xml->icon_url_name”/>’;
    • 06: echo’<h2>’ . $xml->weather . ‘</p>’;
    • 07: echo ‘<br class=“clear” />’;
    • 08: echo ‘<ul class=“nobulletlist”>’;
    • 09: echo '<li><strong>Wind Chill (&#176;F): </strong>' . $xml->windchill_f . '</li>';
    • 10: echo '<li><strong>Heat Index (&#176;F): </strong>' . $xml->heat_index_f . '</li>';
    • 11: echo '<li><strong>Humidity: </strong>' . $xml->relative_humidity . '%</li>';
    • 12: echo '<li><strong>Wind: </strong>' . $xml->wind_string . '</li>';
    • 13: echo '<li><strong>Pressure: </strong>' . $xml->pressure_in . '</li>';
    • 14: echo '<li><strong>Dewpoint: </strong>' . $xml->dewpoint_f . '</li>';
    • 15: echo '</ul>';
    • 16: echo '<p class=&quot;centertxt&quot;><em>' . $xml->observation_time . '</em></p>';
    • 17: echo '<p class=&quot;centertxt&quot;><a href=&quot;http://www.crh.noaa.gov/forecast/ MapClick.php?CityName=Aurora&state=IL&site=LOT&quot;>view forecast</a></p>';
    • 18: } else {
    • 19: echo “<p>Sorry, no weather data available at this time.</p>”;
    • 20: }
  • 13. Weather Display
  • 14. Weather Notes
    • Data and weather image dynamically loaded from NWS site for every single visit
    • Eats up bandwidth and NWS server resources
    • What if data isn’t available due to server offline, fiber cut, service outage, etc.?
    • Takes longer to load from external source, which, in turn, slows down the loading of our page
  • 15. Accessing Data Locally
    • Problem
    External data source getting called with each page load, even though data is only updated once an hour. Solution Load data once, write to local file and only update once an hour.
  • 16. Local Weather Data Outline
    • Copy XML data to local file
    • Store weather images locally
    • Check for existence of local file
    • If it doesn’t exist, we’ll create it
    • If it does, compare last modified date
    • Depending on date, load local data or refresh file from external source
  • 17. Local Weather Data Code 01: // Get Weather Routine // 02: $filename = $_SERVER['DOCUMENT_ROOT'] . '/feeds/KARR.xml'; 03: $weatherurl = 'http://www.nws.noaa.gov/data/current_obs/KARR.xml'; 04: $weatherdata = ‘’; 05: // check to see if the local file exists 06: if (file_exists($filename)) { 07: // get difference in seconds between now and last modified date 08: $diff = (time() - filemtime(&quot;$filename&quot;))/60*60; 09: // if greater than 1 hr (3600 seconds) get new file from source 10: if ($diff >= 3600) { 11: // check to make sure file has write permissions 12: if (is_writable($filename)) { 13: $weatherdata = file_get_contents($weatherurl); 14: file_put_contents($filename,$weatherdata, LOCK_EX); 15: } 16: }; 17: } else { 18: // file doesn't exist, get data and create new file 19: $weatherdata = file_get_contents($weatherurl); 20: file_put_contents($filename,$weatherdata); 21: } 22: //check again to be sure file exists 23: if (file_exists($filename)) { 24: // write it out 25: $xml = simplexml_load_file($filename); 26: // Insert display code here 27: }
  • 18. Local Weather Data Display
    • Visually the same
    • Only one call to the external data is made every hour
    • No calls to external images are made
  • 19. Live Sample with Code
    • http://www.aurora-il.org/sample/xml_sample.php
  • 20. Questions? Ron Pringle Web Developer City of Aurora, IL www.aurora-il.org [email_address] Director, Lakes Region National Association of Government Webmasters www.nagw.org [email_address]

×