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.
- <? Php
- ; $ Wsdl = "miclase.wsdl";
- SoapServer ( $wsdl ) ; $ Soap = new SoapServer ($ wsdl);
- ( 'MiClase' ) ; $ Soap -> setClass ('MyClass');
- ( ) ; $ Soap -> handle ();
- / / Class that manages the ws
- class MyClass {
- public function MyClass () {
- / / Your code
- }
- / **
- *
- * @ Param string $ email
- * @ Return string
- * /
- $email ) { public function is_email_available ($ email) {
- / / Your code ...
- ; return "OK";
- }
- / **
- *
- * @ Param string $ phone
- * @ Param string $ email
- * @ Return string
- * /
- $phone , $email ) { public function register_user ($ phone, $ email) {
- / / Your code ...
- ; return "OK";
- }
- / **
- *
- * @ Param string $ phone
- * @ Return string
- * /
- $phone ) { public function downgrade_user ($ phone) {
- / / Your code ...
- ; return "OK";
- }
- }
- ?>
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
. 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:
- <? Php
- "wsdl2php/WSDLCreator.php" ) ; require_once ("wsdl2php/WSDLCreator.php");
- WSDLCreator ( "miclase" , "http://ws.tudominio.com/wsdl" ) ; $ Test = new WSDLCreator ("myclass", "http://ws.tudominio.com/wsdl");
- ( "miclase.php" ) ; $ Test -> addFile ("MyClass.php");
- ( "http://tudominio.com" ) ; $ Test -> setClassesGeneralURL ("http://tudominio.com");
- ( "MiClase" , "http://ws.tudominio.com/miclase.php" ) ; $ Test -> addURLToClass ("MyClass", "http://ws.tudominio.com/miclase.php");
- ( ) ; $ Test -> createWSDL ();
- ?>
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:
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
.










5 users have commented in "My first webservice in PHP (sparks)"
Feed The comments to this entry TrackbackGreetings, great article, keep it up with the blog, would like the post to be a little more often, but I understand that sometimes missing time, if you need people to send an item from time to time you can post please contact, greetings!
Good Tutorial, I'm starting in the web service, how do I do in php without using nusoap to consume the example you have given?
I've been testing the code in SoapUI, I get the three methods but only in the first I get well defined as a string data type in the WSDL, anyone know why only first method takes either the data type in the other me the left as anyType
I've been testing the proposed tool (WSDL Generator) with the example you exhibit, just changed the names of the methods (method1, method2 and method3 respectively) and the texts of return ("OK" for each of the methods) by " Method1 "," Method2 "and" Method3 ", this to test each of the methods set out properly invoked.
It turns out that when tested with SoapUI (version 4.0.1) only works the first method, called the other methods fail. In SoapUI you can see that the response message for methods 2 and 3 is poorly constructed, which you think is happening?
Thank you.
@ Leonardo,
Wsdl Generator is NOT perfect, has certain problems and should be revised wsdl generated and troubleshoot errors usually are due to improperly characterizes the input / output, the entire structure is correct.
Leave a Reply