Tutorial Thumbnails
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.
thumbnail.ws
This time we will be using a real web service: The screenshot API at https://thumbnail.ws.
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://api.thumbnail.ws/api/. The API documentation can be found at thumbnail.ws.
Sample code in PHP:
<?php //--- Set the parameters --------------// $url = "https://free-web-services.com/"; $apikey = "1234567"; /* FILL IN YOUR FREE API KEY, GET ONE AT thumbnail.ws */ $width = 500; //--- Make the call -------------------// $fetchUrl = "https://api.thumbnail.ws/api/".$apikey ."/thumbnail/get?url=".urlencode($url)."&width=".$width; $jpeg = file_get_contents($fetchUrl); //--- Do something with the image -----// /* file_put_contents("screenshot.jpg", $jpeg); */ header("Content-type: image/jpeg"); echo $jpeg; exit(0); ?>
The output will be a JPEG screenshot of the website.
Service Implementation - SOAP
SOAP, originally an acronym for Simple Object Access protocol, is a protocol specification for exchanging structured information in the implementation of web services in computer networks. It uses XML Information Set for its message format, and relies on other application layer protocols, most notably Hypertext Transfer Protocol (HTTP) or Simple Mail Transfer Protocol (SMTP), for message negotiation and transmission.
Sample code in C#
- Create a new project
- In Form1 add:
- Two text boxes (API Key and URL)
- One button
- One PictureBox
- Add a new Service Reference using address https://api.thumbnail.ws/soap?wsdl
- In Form1.cs add the code below:
using System; using System.Windows.Forms; using System.Drawing; using System.IO; namespace ThumbnailSoap { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { var client = new ServiceReference1.getSoapClient(); string image; client.get( textBox1.Text, textBox2.Text, pictureBox1.Width, "true", out image); //--- Decode base64 image byte[] imageData = Convert.FromBase64String(image); //--- Set image in pictureBox pictureBox1.Image = new Bitmap(new MemoryStream(imageData)); } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); } } } }
The end result will look like this:
Add Tutorial