Sunday, July 15, 2007

PHP webservices

I spend the last couple of days experimenting using php as a webservice client so here are a few tips!

Installation on Debian 4.0


To get up and running you need, a webserver and working php5. It important to have php5 as the webservices pretty much don't work under 4.

apt-get update
apt-get upgrade
apt-get install php5 php-soap php5-xmlrpc apache2 libapache2-mod-php5

edit /etc/php5/apache2/php.ini uncomment

[soap]
; Enables or disables WSDL caching feature.
soap.wsdl_cache_enabled=0
; Sets the directory name where SOAP extension will put cache files.
soap.wsdl_cache_dir="/tmp"
; (time to live) Sets the number of second while cached file will be used
; instead of original one.
soap.wsdl_cache_ttl=86400

Restart apache with "apache2ctl restart" and you should no be calling up php5. You might want to create a php.info apges to test by creating a test.php with the following.

// Show all information, defaults to INFO_ALL
phpinfo();
// Show just the module information.
// phpinfo(8) yields identical results.
phpinfo(INFO_MODULES);
?>

It should show your running php version 5.something and furhter down the page you should be able to see a soap section.

Simple Webservice Test


I use the .Net environment for creating web services and this foray into php was something of a test. It took me a while to get anything done as there are several php implementations of webservices none of which are very well documented. Eventually I saw the post by OrionI and it was enough to access my service. Because of the way the ms service works you need to use the SoapVar command.

In my webservice expects two doubles ratio and tolerance so I did the following

$pRatio = $_POST['xratio'];
$pTolerance = $_POST['xtolerance'];
include("SOAP/Client.php");
$namespace = "http://mars.glenfernassociates.co.uk/enmesh";
$wsdl = "http://10.0.0.254:8080/GearService.asmx?wsdl";
$sc = new SoapClient($wsdl);
$ratio = new SoapVar($pRatio, XSD_DOUBLE, "double", $namespace);
$tolerance = new SoapVar($pTolerance, XSD_DOUBLE, "double", $namespace);
$wrapper->ratio = $ratio;
$wrapper->tolerance = $tolerance;
$params = new SoapParam($wrapper, "GetPair");
$ret = $sc->GetPair($params);
?>

No comments: