Abstract
Like in many tutorial sites, it might be able to add a “tryit” editor to let your clients try your webservices without installing any client.
This little note explains how I built a basic TryIt editor for Soap webservices.
I illustrate with a public soap currency converter SOAP api found on the internet (Kowabunga.net currency converter)
Principle
We expose a form, and the user pastes his SOAP command in his browser. He can also change the URL
The application will just add necessary headers to the request, and repost the contents to the backend server.
Note that no extra security is provided. The only added value of this page is to let the developer focus on the contents of the ws call – hiding the headers complexity.
I also don’t use PHP soap functions : if the user makes a mistake in his message, the faulty message will get to the backend, the the user will receive the backend error (instead of a PHP error). This means that the editor page may also be used to post to a JSON/REST endpoint with very few changes.
Installation php curl
The page uses curl to post messages. PHP provides a interface to libcurl, but it is not installed by default. Ubuntu provides a *.deb package, so I don’t bother with pip :
sudo apt-get install php7.0-curl
TryIt webpage
The form
I just create a form with 3 columns (input, tryit button and output), and the destination URL. When tryit button is pressed, it posts the “input” textarea contents to the same page :
<form method="POST" action="tryit.php">
<table width="100%" border="1px">
<tr>
<td colspan="3">
URL : <input type="string" width="100%" id="url" name="url" value="http://currencyconverter.kowabunga.net/converter.asmx"></input><br/>
</td>
</tr>
<tr>
<td width="40%">
<textarea name="input" id="input" rows="10" cols="100" ><?php echo htmlspecialchars($_POST['input']);?></textarea>
</td>
<td width="20%">
<input type="submit" value="try it"></input>
</td>
<td width="40%">
<textarea id="output" name="output" rows="10" cols="100"><?php echo htmlspecialchars($response);?></textarea>
</td>
</tr>
</table>
</form>
The goal is to post the form the same page (tryit.php) – If you call the page from a browser, the two highlighted “echo” commands will return nothing, and the form will be empty.
Communication to backend
When the user clicks on “Try It” button, the same page will be called from an HTTP POST command. When this occurs, we want to call the backend, and show to the user the request and the response. Here is the code that executes this
<?php
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL,$_POST["url"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS,$_POST["input"]);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=ISO-8859-1'));
$response = curl_exec($ch);
curl_close($ch);
?>
The contents of “input” is posted to the backend, whose URL is also provided in “url”. I also add a mandatory soap header (‘Content-Type’)
Putting everything together
If you paste the form and the php code to your webpage, you have your “tryit” editor.
You can for example list the currencies known by the library with the following soap message :
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"> <soapenv:Header/> <soapenv:Body> <tem:GetCurrencies/> </soapenv:Body> </soapenv:Envelope>
Not too nice, but working –