26 Sep 09

The web has evolved in many ways. From the static html web to the dynamic pages and from the publisher’s content to the user created content (web 2), the web has gone through many stages. As mentioned at the Service Web 3.0 project, the next web stage will be web services driven. We will be able to connect any device with the internet and manage this device remotely.

So, if web services is the future, you should be able to create and use them so that it will be easier for you to follow the evolution it will bring. We will try to give you some info about web services using SOAP. How to create a service, how to use it from a web based or a desktop application and some known web services that might come handy.

The Tools

We will be using some tools. The first tool we need to have is soapUI.

soapui

soapUI is a free and open source desktop application for

  • inspecting Web Services
  • invoking Web Services
  • developing Web Services
  • Web Services Simulation and Mocking
  • Functional, Load and Compliance testing of Web Services

It is mainly aimed at developers and testers providing or consuming WSDL or REST based Web Services (Java, .net, etc). Functional and Load Testing can be done both interactively in soapUI or within an automated build or integration process using the soapUI command line tools.
Mock Web Services can easily be created for any WSDL and hosted from within soapUI or using the command-line MockService runner. IDE-plugins are available for

  • eclipse plug in
  • IntelliJ IDEA plug in
  • NetBeans plug in

soapUI requires Java 1.5 and is licensed under the LGPL license.

We will also need a PHP library that will make our life easier. We will be using NuSOAP for this. NuSOAP is a rewrite of SOAPx4, provided by NuSphere and Dietrich Ayala. It is a set of PHP classes – no PHP extensions required – that allow developers to create and consume web services based on SOAP 1.1, WSDL 1.1 and HTTP 1.0/1.1.

And last but not least, Microsoft Visual C++ Express Edition in order to create our desktop client. You might also need to get a smilies pack, if you don’t have one.

The Challenge

We need a web service that will accept simple text as argument and it will return an image url depending on the command given. Some of you might have guessed that this service is a smilies web service :)

The Database Structure

We need only one table for this service. Create a new database named servesmiles and add a new table named smilies.

Table smilies:

CREATE TABLE IF NOT EXISTS `smilies` (
 `id` int(11) NOT NULL auto_increment,
 `image` varchar(250) collate utf8_unicode_ci NOT NULL,
 `code` varchar(50) collate utf8_unicode_ci NOT NULL,
 PRIMARY KEY  (`id`),
 KEY `code` (`code`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

The Directory Structure

Create a directory named smileserv and 1 subfolder named smilies and place your smilies in it. We will also place the NuSOAP library in a folder named nusoap. There are 2 files we need to create too. The first one named server.php and the second one named index.php :

dirs

The Service

The service is a simple function that will query the database and return the smilie path. We don’t include a password cause we test it local. You might need a password if you use it online.

function getSmilie($text){
 //connect to the database
 $conn = new PDO('mysql:host=localhost;dbname=servesmiles', 'root', '');
 //setup the sql statement
 $sql = "SELECT * FROM `smilies` WHERE `code`=?";
 $data = array($text);
 //prepare
 $sth = $conn->prepare($sql);
 //execute
 $sth->execute($data);
 //return the image
 $result = $sth->fetch();
 return $result['image'];
}

So, our function will look into the database table for the image code provided and it will return the smilie image path. Next thing to do is to create the web service server file.

The Server

Creating a server is very easy using the NuSOAP library. All you have to do is to include the nusoap library and register the methods you will use:

// load SOAP library
require_once("./nusoap/nusoap.php");
// load library that holds implementations of functions we're making available to the web service

// set namespace
$ns="http://localhost";
// create SOAP server object
$server = new soap_server();
$server->soap_defencoding = "utf-8";
$server->decode_utf8 = false;
// setup WSDL file, a WSDL file can contain multiple services
$server->configureWSDL('JeezApi',$ns);
$server->wsdl->schemaTargetNamespace=$ns;
// register a web service method
$server->register('smileme',
 array('smilecode' => 'xsd:string'),     // input parameters
 array('smilepath' => 'xsd:string'),     // output parameter
 $ns
 );

function smileme($smilecode){
 //include the service file
 include "./index.php";
 return new soapval('smilepath','xsd:string',getSmilie($smilecode));

}// service the methods
$server->service($HTTP_RAW_POST_DATA);

Now we are almost ready. You can check what your server.php file does by opening it in your browser:

http://localhost/smileserv/server.php:

api1

http://localhost/smileserv/server.php?wsdl :

wsdl

So, we created a web service server with less than 27 lines of code, using the NuSOAP library.

Testing the Service

We need to test the service. This is where soapUI comes into play. Load soapUI and create a new project:

soapuinewproject

Make sure you fill in the correct WSDL URI and press the OK button.

If everything is ok, you should see something like this:

soapuiproj

Now, if you double click on the Request 1 option, a new window will open that looks like this:

testsoap

Try clicking the green arrow on top left of this window. You will notice that you get an empty response. This is because there are no data in our database table. Try inserting an image name and a code in the smilies table. For the example bellow we will use “:P” as a smilie code that will point to the tongue.gif file (you can find it in the smilies package provided above):

INSERT INTO `servesmiles`.`smilies` (
`id` ,
`image` ,
`code`
)
VALUES (
NULL , 'tongue.gif', ':P'
);

Now lets try our service. Load the Request 1 option from soapUI and replace the “?” with “:P”. Press the green arrow button on the top left and the window will look like this:

soapresult

This means that our service works as it should. Now we should see how we can use the service from another site using PHP.

A PHP Client

Creating a client for the service is even easier. The way to go is similar to the server creation process. You include the NuSOAP library and create a new client object.

File client.php:

//include the nusoap library
include './nusoap/nusoap.php';
//set the wsdl file src
$wsdl="http://localhost/smileserv/server.php?wsdl";
//and create a client
$c=new nusoap_client($wsdl,'wsdl');
$t=array('smilecode'=>':P');
//then call the method smileme
$file = $c->call('smileme', $t);
//and echo the image
echo "<img src='http://localhost/smileserv/smilies/$file' />";

You can now try the client. Point your browser to the client.php file and you will be able to see the tongue.gif file

A C++ Client

We will create a C++ client using Microsoft Visual C++ Express edition. Create a windows forms project with 1 textbox 1 button and a webbrowser control:

cppclient

Next, add a new web reference to your project:

addref

Fill in the url to the server.php file. Don’t forget to add the wsdl parameter:

cppwebref

Now double click the “Hit me!” button and put this code in the button1_Click method:

localhost::JeezApi^ proxy =
 gcnew localhost::JeezApi();
 this->webBrowser1->Navigate(System::String::Concat("http://localhost/smileserv/smilies/",proxy->smileme(this->textBox1->Text)));

Thats it! Now Run your client and fill in the “:P” code we set in the database:

client

I hope this tutorial was fun to read (:

Some Known Web Services

Yahoo! offers some great web services. You can use the Yahoo! network to create your own search engine, spell checkers and many more. Microsoft also offers Bing services and Google does also. Nearly all known networks (facebook, Twitter, Flickr etc) provide web services.

You can also find interesting web services with the seekda web services directory. For example the Excel web service.

VN:F [1.8.1_1037]
Rating: 3.9/10 (10 votes cast)
VN:F [1.8.1_1037]
Rating: +2 (from 4 votes)
Web 3.0 Will Be All About Web Services. Learn The Basics3.91010

Popularity: 4%

  • Share/Bookmark

Related posts:

  1. Learn Programming With Youtube There are many ways to start your journey in the...
  2. A Jeez.eu implementation of an URL Shortener Two days ago a friend of mine suggested to me...
  3. Programming in the Cloud. Some remarkable Online IDEs. Cloud computing is said to be the next big thing....
  4. Using YQL To Manage Your Yahoo! meme Account Yahoo! Query Language is an expressive SQL-like language that lets...
  5. Using APIs With PHP? Here Are Your Classes Do you want to use an API but you are...

About the Author:

Filed under: Services, Tutorials - Trackback Uri

Comments are closed.