For all your music news related needs


Week 12:

Web services are particular pieces of software which usage has been made available to over the internet. Web services typically communicate via open protocols and have an XML basis. There are two forms of web services, these are SOAP (Simple Object Access Protocol) web services, and RESTful web services. RESTful APIs make use of HTTP requests in order to GET, POST, PUT, and delete data. SOAP is typically more robust however RESTful systems are often used because they use less bandwidth which is ideal for and web usage. SOAP is an XML based protocol which is designed for use over HTTP, similar to RESTful however it is said that with the introduction of REST, using web services is made considerably easier.

Web services can range from weather information to live stock market conditions and much more, often allowing you to highlight areas of interest for detail. A weather web service called Open Weather Map was implemented which allowed for dynamic readings of weather conditions in a specified location, in this case, Munich.

In order to use this web service, you must have a user account and be registered on the Open Weather Map website. You then must apply for an API key which will allow you to make data requests. cURL statements are curated for the HTTP request allowing you to recieve the desired response. The data is then put through a json_decode for further manipulation.

Since the data has been decoded, it is possible to access these pieces of data as though it is any other array. The following code shows both how the data is requested and how it is displayed on the screen.

$apiKey = "0db44b8881747a7c4b239f222cbaea03";

$cityId = "2867714";

$googleApiUrl = "http://api.openweathermap.org/data/2.5/weather?id=" .

$cityId . "&lang=en&units=metric&APPID=" . $apiKey;

$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_URL, $googleApiUrl);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch, CURLOPT_VERBOSE, 0);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$response = curl_exec($ch);

curl_close($ch);

$data = json_decode($response);

$currentTime = time();


<h2><?php echo $data->name; ?>'s Weather Status</h2>

<div class="time">

 <div><?php echo date("l g:i a", $currentTime); ?></div>

 <div><?php echo date("jS F, Y",$currentTime); ?></div>

 <div><?php echo ucwords($data->weather[0]->description); ?></div>

</div>

<div class="weather-forecast">

 <img

 src="http://openweathermap.org/img/w/<?php echo $data->weather[0]->icon; ?>.png"

 class="weather-icon" /> <?php echo "<br>High: ".$data->main->temp_max; ?>°C<span

 class="min-temperature"><?php echo " Low: ".$data->main->temp_min; ?>°C</span>

</div>

<div class="time">

 <div>Humidity: <?php echo $data->main->humidity; ?> %</div>

 <div>Wind: <?php echo $data->wind->speed; ?> km/h</div>

</div>

Using web services is a lot like code reuse, you can save a significant amount of development time writing specialised pieces of software to achieve a task or you can use existing sources, in this case, the web service is in such a format that they give you everything you need in terms of data without the worry of dealing with the source code. The only disadvantage to this is that the system may not achieve exactly what you desire however the majority of preexisting systems such as Open Weather Map's API is very inclusive and can help achieve various different tasks. Using this system is also beneficial because the creators will often have an abundance of data and will have many stakeholders helping ensure that everything is correct and in working order.

word count: 409