Tuesday, 25 December 2007

SOAP: PHP5 and nusoap.php

I've been learning today some SOAP (Service Oriented Architecture Protocol), the protocol for exchanging XML-based messages over computer networks, normally using HTTP.

Also, the simpliest way to make it work without spending hours reconfiguring PHP on your server, is to use nusoap.php. This set of classes is 100% written in PHP and work out of the box with PHP4, but not PHP5! Also, to make it work, you will have to edit the file nusoap.php by changing:

1. Do a search and replace of soapclient to soap_client in the entire document
2. Replace this code:

if (!extension_loaded('soap')) {
/**
* For backwards compatiblity, define soap_client unless the PHP SOAP extension is loaded.
*/
class soap_client extends nusoap_client {
}
}

By this code:

class soap_client extends nusoap_client {}

And now it's time to try your first SOAP script (from http://dietrich.ganx4.com/nusoap/faq.php including the update of the class):

Here is a simple "hello world" example. First create file "hello.php."
Put nusoap.php in the same directory, or in your include directory, or enter
the path to them in the require_once() calls below. Then paste the code below
into the file. That's it!

require_once('nusoap.php');
$server = new soap_server;
$server->register('hello');
function hello ($name)
{
return "Hello $name.";
}
$server->service($HTTP_RAW_POST_DATA);

Accessing the service via a NuSOAP client:

require_once('nusoap.php');
$soap_client = new soap_client('http://yourdomain.com/hello.php');
echo $soap_client->call('hello',array('name'=>'dietrich'));

3 comments:

Jimmy said...

nusoap, c'est pas mal, dans mon ancienne boite j'avais écrit un service qui récupérait un web service de Taobao, j'avais beaucoup appris sur ce projet ... et j'ai surtout appris à détesté bossé avec un développeur chinois qui ne document pas du tout son webservice, dans mon cas on pouvait parler d'un websévice !

hamdi.Daoud said...

bjr
votre code incomplet la définition du service , ca e marche pas comme ca!!
en plus tu doit initialiser le WSDL pour que le service soit pris en compte
sinon un soap fault qui tu récupère

hamdi.Daoud said...

bjr
votre code incomplet la définition du service , ca e marche pas comme ca!!
en plus tu doit initialiser le WSDL pour que le service soit pris en compte
sinon un soap fault qui tu récupère