Tutorial
We received a lot of question to add a how-to or tutorial to this website. This tutorial will show you how to consume a web service in different programming environments.
The Web service
First of all we need a web service to use. We have set up a a web service called add. This service will take two integers as input and return the sum.
Service Implementation - HTTP GET
HTTP GET is the easiest way to consume a web service: call a URL, add some parameters and parse the output. In this case the service end point is at https://free-web-services.com/add and the parameters must be named 'a' and 'b'. You can easily check the output by clicking on the next URL: https://free-web-services.com/add?a=5&b=7 .
Sample code in PHP:
<?php $a = 4; $b = 9; $url = 'http://free-web-services.com/add?a=' . urlencode($a) . '&b=' . urlencode($b); $output = file_get_contents($url); echo $output; ?>
The output will be:
13
Service Implementation - JSON (HTTP POST)
The same service end point can be used to POST a JSON object. The result will be a new JSON object with two properties: sum (the actual sum) and time (the time needed to calculate the sum).
Sample code in PHP:
<?php $obj = new StdClass(); $obj->a = 4; $obj->b = 9; //--- Use curl to do the HTTP-POST transport $ch = curl_init(); curl_setopt($ch,CURLOPT_URL, 'http://free-web-services.com/add'); curl_setopt($ch,CURLOPT_POST, true); curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($obj)); //--- Execute $result = curl_exec($ch); //--- Get the HTTP result $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE); //--- Close curl handle curl_close($ch); if ($httpStatus == 200) { //--- Success $resultObj = json_decode($result); echo "Sum = " . $resultObj->sum . "\n"; echo "Time = " . $resultObj->time . "\n"; } else { //--- Error echo "Server returned error " . $httpStatus . ".\n"; } ?>
The output will be:
Sum = 13
Time = 0.002
Service Implementation - SOAP
Soap is more complex. But when you use a development enviroment that offers out-of-the-box soap clients it is actually very simple. The "add" service is described in https://free-web-services.com/add.wsdl. This is an XML document describing the transport methods, input and output messages and available operations. In this case only the "add" method is available.
Create a new command line project in C# and add a
service reference:
Then create a small program that uses this service
reference:
namespace ExampleProgram1 { class Program { static void Main(string[] args) { var client = new ServiceReference1.addSoapClient(); double time; int sum = client.add(13, 17, out time); System.Console.Out.WriteLine("sum = " + sum); System.Console.Out.WriteLine("time = " + time); } } }
Or a similar program in PHP:
<?php $client = new SoapClient('http://free-web-services.com/add.wsdl'); try { $response = $client->add(array("a" => 13, "b" => 17)); echo "sum = " . $response->sum . "\n"; echo "time = " . $response->time . "\n"; } catch (SoapFault $fault) { echo "Error: " . $fault->faultcode . ": " . $fault->getMessage() . "\n"; }
In both programs the output should be:
sum = 30
time = 0.0002
Thumbnails Tutorial