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));


?>

3 comments:

Trusted Mobile App Development Company. said...

Very useful information for
PHP Developers

CMTech said...

Thanks for sharing a valuable post and its really very helpful.

PHP Web Development Brisbane

Alisha Khan said...

Really nice and interesting post.

website development services