Creating Web Services with PHP and SOAP
There are two basic approaches that are used to create web services from scratch. In this first of a two part series on web services I’ll talk about the 'top-down' approach. According with this approach, we need to produce the XML description of the service before it is implemented. So the service is fully described in terms of what it does, how it can be called and what result(s) it returns, but is not actually implemented. This (WSDL) specification is then used as a guide to writing the code that implements the service.
The project structure
The webservice will receive a string parameter and it will return the 'hello' string concatenated with the input parameter. For example, if the input is 'Andrea', then the output will be 'Hello Andrea'. In order to achieve this, we need to define the WSDL descriptor.
The descriptor.wsdl
<?xml version="1.0" encoding="UTF-8"?> <wsdl:definitions name="HelloWS" targetNamespace="http://ws.rolandopalermo.com/" xmlns:tns="http://ws.rolandopalermo.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> <wsdl:types> </wsdl:types> <wsdl:message name="helloRequest"> <wsdl:part name="name" type="xsd:string"></wsdl:part> </wsdl:message> <wsdl:message name="helloResponse"> <wsdl:part name="result" type="xsd:string"></wsdl:part> </wsdl:message> <wsdl:portType name="HelloWS"> <wsdl:operation name="hello"> <wsdl:input message="tns:helloRequest"/> <wsdl:output message="tns:helloResponse"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="HelloWSPortBinding" type="tns:HelloWS"> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="hello"> <soap:operation soapAction="http://localhost/soap_demo/server.php"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="HelloWS"> <wsdl:port name="HelloWSPort" binding="tns:HelloWSPortBinding"> <soap:address location="http://localhost/soap_demo/server.php"/> </wsdl:port> </wsdl:service> </wsdl:definitions>We also need to implement the hello method in a php file.
The server.php file
<?php // turn off WSDL caching ini_set("soap.wsdl_cache_enabled","0"); function hello($name) { return 'Hello '.' '.$name; } // initialize SOAP Server $server = new SoapServer("descriptor.wsdl",[]); // register available functions $server->addFunction('hello'); // start handling requests $server->handle();Now, we can import the WSDL file into SoapUI for example and the results should look like this:
The SOAP request.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.rolandopalermo.com/"> <soapenv:Header/> <soapenv:Body> <ws:hello> <name>Andrea</name> </ws:hello> </soapenv:Body> </soapenv:Envelope>The SOAP response.
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Body> <SOAP-ENV:helloResponse> <result>Hello Andrea</result> </SOAP-ENV:helloResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope>Do not forget to enable the SOAP extension in the php.ini file. To do this, you have to find extension=php_soap.dll in php.ini, remove the semicolon(;) and restart the server.
Comentarios
Publicar un comentario