After much time consuming webservices other touched me create my first PHP SOAP server and, frankly, it seemed really simple and intuitive. You create a class with methods that are going to exhibit at the ws and the service is automatically created for them, as simple as that.

  1. <? Php
  2. ; $ Wsdl = "miclase.wsdl";
  3. SoapServer ( $wsdl ) ; $ Soap = new SoapServer ($ wsdl);
  4. ( 'MiClase' ) ; $ Soap -> setClass ('MyClass');
  5. ( ) ; $ Soap -> handle ();
  6. / / Class that manages the ws
  7. class MyClass {
  8. public function MyClass () {
  9. / / Your code
  10. }
  11. / **
  12. *
  13. * @ Param string $ email
  14. * @ Return string
  15. * /
  16. $email ) { public function is_email_available ($ email) {
  17. / / Your code ...
  18. ; return "OK";
  19. }
  20. / **
  21. *
  22. * @ Param string $ phone
  23. * @ Param string $ email
  24. * @ Return string
  25. * /
  26. $phone , $email ) { public function register_user ($ phone, $ email) {
  27. / / Your code ...
  28. ; return "OK";
  29. }
  30. / **
  31. *
  32. * @ Param string $ phone
  33. * @ Return string
  34. * /
  35. $phone ) { public function downgrade_user ($ phone) {
  36. / / Your code ...
  37. ; return "OK";
  38. }
  39. }
  40. ?>

This will automatically create our webservice with the three public methods. But wait, something is missing, above all define a 'miclase.wsdl ". What is that? Where does?

In fact, that is the main problem when creating a SOAP webservice with PHP, WSDL is not generated automatically but you have to write in Hand. To fix we Bookseller PHP WSDL Generator which must only pass the class from which we extract the WSDL and does it for us icon smile Mi primer webservice en PHP (chispas) webservice soap PHP . That everything works well it is necessary that our class methods are well documented as they appear in the above example, so WSDL Generator know configure the types of data input and output parameters of the methods.

Here's an example:

  1. <? Php
  2. "wsdl2php/WSDLCreator.php" ) ; require_once ("wsdl2php/WSDLCreator.php");
  3. WSDLCreator ( "miclase" , "http://ws.tudominio.com/wsdl" ) ; $ Test = new WSDLCreator ("myclass", "http://ws.tudominio.com/wsdl");
  4. ( "miclase.php" ) ; $ Test -> addFile ("MyClass.php");
  5. ( "http://tudominio.com" ) ; $ Test -> setClassesGeneralURL ("http://tudominio.com");
  6. ( "MiClase" , "http://ws.tudominio.com/miclase.php" ) ; $ Test -> addURLToClass ("MyClass", "http://ws.tudominio.com/miclase.php");
  7. ( array ( "MiClase" => "MiClase" ) ) ; $ Test -> ignoreMethod ( array ("MyClass" => "MyClass"));
  8. ( ) ; $ Test -> createWSDL ();
  9. ( dirname ( __FILE__ ) . "/miclase.wsdl" , false ) ; $ Test -> saveWSDL ( dirname (__ FILE__). "/ miclase.wsdl", false);
  10. ?>

This little code will generate the WSDL file from our webservice. You see just the file we tell our class (which we wrote above), the class that we want to map the webservice URL (endpoint) and in addition, we show that ignores the class constructor it will not be a method our webservice. That's it.

Now try the web service, for example from the Eclipse Web Service Explorer:

wseclipse Mi primer webservice en PHP (chispas) webservice soap PHP After giving the path to the wsdl, http://ws.tudominio.com/miclase.php?wsdl, see the three methods we have discussed and we can try and use them.

Never had the need to create a SOAP server but it has been really easy. Now I'm looking for ways to return complex data types, but that will be in the next chapter icon razz Mi primer webservice en PHP (chispas) webservice soap PHP .