GEOG 863
GIS Mashups for Geospatial Professionals

Consuming Web Services

PrintPrint

In the last section, we saw how to acquire data that is generally not meant to be "mashed up." Here we'll take a look at the easier case -- that probably would have made more sense to do first :-) -- in which the data publisher has made the data available in a form that allows it to be easily incorporated into other applications. XML is a popular format for this kind of data dissemination, which is often referred to as a web service or data feed.

Again, JavaScript can't be used to read remote XML files, but if you have access to a server with PHP, it is not terribly difficult to create a proxy script that simply repackages the XML for consumption by the JavaScript. For example, the (U.S.) National Weather Service provides data feeds of current weather conditions, forecasts and watches/warnings for hundreds of stations nationwide. For example, the data for State College, PA can be found at NOAA.

In the web scraping example on the previous page, the file_get_contents() function was used to read in the contents of the remote file.  That function is actually typically disabled by most server administrators nowadays, so the example below uses a PHP library called cURL to handle the reading of the remote file.

https://php.scripts.psu.edu/jed124/read_xml.php

<?php
function curl_get_contents($url) {
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0");
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}

header("Content-type: text/xml");
$root = 'http://w1.weather.gov/xml/current_obs/';
$data = curl_get_contents($root ."KUNV.xml","r");

$data = str_replace('<?xml-stylesheet href="latest_ob.xsl" type="text/xsl"?>','',$data);

echo $data;
?>

Earlier in the course, you read about Ajax and saw an example map in which PHP was used to retrieve data from a database.  The data outputted by that PHP script were consumed within the JavaScript code using a downloadUrl() function written in a special library intended to simplify the usage of the XmlHttpRequest object that makes Ajax possible.  Just as in that example (and others found in the optional database lesson), the name of the proxy script above can be plugged into a downloadUrl() statement to supply the source data for a custom Google Map.

That brings us to the end of the topics I wanted you to be exposed to before beginning your final project. On the next page, I list a number of project ideas for those who can't come up with an idea of their own. Even if you have a good project idea, you may find it helpful to read through this page to get a better sense of my expectation level.