Showing posts with label web service client. Show all posts
Showing posts with label web service client. Show all posts

Thursday, September 25, 2008

PHP Web Service example.

Its been a while since I used PHP. The last time I used it was PHP 4. I felt it was time to revisit the language. I have good memories of PHP. It was better than perl and it worked reasonably well for a mediawiki installation I maintained.

If you view the other post I've made, you'll see that a first test of a language that I like to do is to see if it supports a simple SOAP call. This tutorial will be the same test for PHP.

First lets install it. If you use any *nix system you'll probably already have it installed. If not or if you want the latest copy then go to http://www.php.net/downloads.php and download the version for you install. I chose the windows installer. During the setup process it asked which extensions I would like installed I opted for the SOAP library which will be needed for the rest of the tutorial.

I installed PHP in the default location of c:\program files\php, so I opened a command prompt and browsed to that location.

Now for our simple web service test. We'll be accessing 'http://webservices.daehosting.com/services/isbnservice.wso?WSDL' and asking if a given ISBN is valid or not.

The first task is create a new SOAPClient object.


$client = new SOAPClient('http://webservices.daehosting.com/services/isbnservice.wso?WSDL');


Now we call our method which is named '>IsValidISBN13'. Like perl's SOAP::Lite we need to give a hash as our argument so the web service knows which variable were passing so we first create a parameter list, then call the function.



$params = array('sISBN'=>"9781565926103");
$result = $client->IsValidISBN13($params);




So far so good. Now the only tricky part I found of the process. The PHP Soap libraries return a stdClass. We need to unravel this into something meaningful to us. The documentation uses this function so I did as well.


function obj2array($obj) {
$out = array();
foreach ($obj as $key => $val) {
switch(true) {
case is_object($val):
$out[$key] = obj2array($val);
break;
case is_array($val):
$out[$key] = obj2array($val);
break;
default:
$out[$key] = $val;
}
}
return $out;
}



Then lastly I just print out the array which contains our result:


print_r(obj2array($result));



You can now experiment by passing different ISBN's ones that are both valid and invalid and you'll see the result is different. You can also call the method that checks for validity of a 10 character length ISBN.

Here is the entire code listing.

<?php

$client = new SOAPClient('http://webservices.daehosting.com/services/isbnservice.wso?WSDL');
$params = array('sISBN'=>"9781565926103");
$result = $client->IsValidISBN13($params);

function obj2array($obj) {
$out = array();
foreach ($obj as $key => $val) {
switch(true) {
case is_object($val):
$out[$key] = obj2array($val);
break;
case is_array($val):
$out[$key] = obj2array($val);
break;
default:
$out[$key] = $val;
}
}
return $out;
}


print_r(obj2array($result));


?>

Tuesday, June 24, 2008

Python web service client using soaplib

Here is an example of a python web service client using soaplib. I personally find the SOAPpy implementation better.
from soaplib.wsgi_soap import SimpleWSGISoapApp
from soaplib.service import soapmethod
from soaplib.serializers.primitive import String
from soaplib.client import make_service_client
class YYY(SimpleWSGISoapApp):
__tns__ = 'http://webservices.daehosting.com/ISBN'
@soapmethod(String,_returns=String,_outVariableName='IsValidISBN13Result')
def IsValidISBN13(self,sISBN):
pass
client = make_service_client('http://webservices.daehosting.com/services/isbnservice.wso?WSDL',YYY())
print client.IsValidISBN13('0000000000000')
print client.IsValidISBN13('9780061374227')

Saturday, June 14, 2008

Perl - SOAP Client (using SOAP::Lite)

This Post shows how to use Perl as a SOAP Client to an existing web service.

For more information on Perl and WebServices I recommend: Programming Web Services with Perl

To access SOAP web services with Perl, I recommend the SOAP::Lite CPAN module. If you don't have SOAP::Lite already installed you can install it by issuing the following commands and answering any prompts.


perl -MCPAN -e shell
CPAN> install SOAP::Lite


Here is a simple example perl call that calls the stock quote webservice at http://www.webservicex.net/stockquote.asmx?wsdl.


use strict;
use SOAP::Lite +trace => 'debug';
my $soap = SOAP::Lite->new();
my $serializer = $soap->serializer();
my $service = $soap->service('http://www.webservicex.net/stockquote.asmx?wsdl');


print $service->GetQuote("nvda");

Note: The recent version of SOAP::Lite appears to have a bug, when the service is a newer one like a .NET or axis the above code doesn't work. I can't find any indication that SOAP::Lite developers are going to fix this or not. If anyone has any information let me know.

Python web service using SOAPPy

Here is an example of how to call a WebService from Python. For more information on Python and Web Services I recommend the following book: Python Cookbook.

This requires the fpconst and SOAPPy package. You can download them from http://research.warnes.net/projects/RStatServer/fpconst/ and http://prdownloads.sourceforge.net/pywebsvcs/SOAPpy-0.11.6.tar.gz?download. Unzip the packages then run python setup.py install on each package.

The following code will then connect to the web service and execute the remote method.

import sys

from SOAPpy import WSDL

url = 'http://webservices.daehosting.com/services/isbnservice.wso?WSDL'
server = WSDL.Proxy(url);
print server.IsValidISBN13(sISBN ='0000000000000')



Note there is another web service package called ZSI, I found SOAPPy to be easier to use, but from what I understand both packages are going to merge together.

Java Soap Client using axis library

This post is an example Java Soap client call using the apache axis library.

First you need to download axis from ws.apache.org. This library provides tools to convert a wsdl into a Java stub class that you can then use.

Once you've extracted the axis directory run the following command on the wsdl that you want to consume.

java -cp axis-1_4/lib/wsdl4j-1.5.1.jar:axis-1_4/lib/saaj.jar:axis-1_4/lib/jaxrpc.jar:axis-1_4/lib/axis.jar:axis-1_4/lib/commons-logging-1.0.4.jar:axis-1_4/lib/commons-discovery-0.2.jar:. org.apache.axis.wsdl.WSDL2Java http://webservices.daehosting.com/services/isbnservice.wso?WSDL


If you have your classpath set up you probably don't need as much but for simplicity I included all the jars in the axis distribution.

Now you can use the stubs that were generated


import com.daehosting.webservices.ISBN.*;

class client {

public static void main(String argv[]) throws Exception {

ISBNServiceSoapType service = new ISBNServiceLocator().getISBNServiceSoap();

System.out.println(service.isValidISBN13("0000000000000"));

}

}


For further reading I recommend the following book Java Web Services.

Ruby SOAP Client

Here is an example of how to call a web service using Ruby. This example requires no external libraries, everything is built into the language. However it does require version 1.8.5 early version don't have the create_rpc_driver method.


#Requires Ruby version 1.8.5 or highet
require 'soap/wsdlDriver'
wsdl = 'http://webservices.daehosting.com/services/isbnservice.wso?WSDL'
driver = SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver

# Log SOAP request and response
driver.wiredump_file_base = "soap-log.txt"

response = driver.IsValidISBN13(:sISBN => '0000000000000')
puts response.isValidISBN13Result


Let's examine the code a bit more.

The first part brings in the required headers, namely soap/wsdlDriver

We then declare a variable wsdl to point to the web service description. The wsdl allows the client code to understand what method are available and how to call them.

We then turn on some debugging, this is optional but if its your first time I recommend it.

drive.isValidISBN13 actually exectues our call. Based on the wsdl it knows how to serialize the call and get back the results which I store in the 'response' variable.

That's It.

I found Ruby to be one of the easiest languages to make a web call from. It doesn't require building any complex wrappers and has all the libraries built it.

If you have any trouble getting it working, leave a comment and I'll be happy to take a look.

For more information about Ruby and Web Services I recommend Ruby Cookbook (Cookbooks (O'Reilly)).