Build a Web service using PHP
Web services provide a standard means of communication between different software applications, running on a variety of platforms and/or frameworks. Web services plays important role in any SOA architecture. I worked with couple of web services project that were developed using Java/J2EE. I was wondering how the same services can be implemented in PHP. Though I didn’t feel the web services support in PHP is as strong as Java and other technologies, but we can work with it. I tried web services functionality that was built in with PHP but later zeroed in on the third party library NuSOAP. NuSOAP provides single php file that you have to include in your code and your ready to implement web services. Here I have tried simple web service example in PHP, with a soap server and soap client.
Lets Build a Web service using PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<?php require('lib/nusoap.php'); function add($param1, $param2) { return array('return'=>$param1+$param2); } function multiply($param1, $param2){ return array('return'=>$param1*$param2); } // Create the server instance $server = new soap_server(); $ns = "http://localhost/MathOperationService"; // Initialize WSDL support $server->configureWSDL('MathOperationService', $ns,'','document'); // Register the method to expose $server->register('add', // method name array('param1' => 'xsd:string', 'param2' => 'xsd:string'), // input parameters array('return' => 'xsd:string'), // output parameters $ns, // namespace "$ns#add", // soapaction 'document', // style 'literal', // use 'Add Parameters' // documentation ); // Register the method to expose $server->register('multiply', // method name array('param1' => 'xsd:string', 'param2' => 'xsd:string' ), // input parameters array('return' => 'xsd:string'), // output parameters $ns, // namespace "$ns#multiply", // soapaction 'document', // style 'literal', // use 'Multiply Parameters' // documentation ); // Use the request to (try to) invoke the service $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ''; $server->service($HTTP_RAW_POST_DATA); ?> |
You can see I have included single third party nusoap.php to achieve this functionality of SOAP Server. NuSOAP provides wsdl support easily. To see the wsdl you need to type in this url in the browser http://localhost/soap-server.php?wsdl. You will be able to see wsdl xml like belwo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
<?xml version="1.0" encoding="ISO-8859-1"?> <definitions xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://localhost/MathOperationService" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://localhost/MathOperationService"> <types> <xsd:schema elementFormDefault="qualified" targetNamespace="http://localhost/MathOperationService" > <xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" /> <xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" /> <xsd:complexType name="addRequestType"> <xsd:all> <xsd:element name="param1" type="xsd:string" form="unqualified"/> <xsd:element name="param2" type="xsd:string" form="unqualified"/> </xsd:all> </xsd:complexType> <xsd:complexType name="addResponseType"> <xsd:all> <xsd:element name="return" type="xsd:string" form="unqualified"/> </xsd:all> </xsd:complexType> <xsd:complexType name="multiplyRequestType"> <xsd:all> <xsd:element name="param1" type="xsd:string" form="unqualified"/> <xsd:element name="param2" type="xsd:string" form="unqualified"/> </xsd:all> </xsd:complexType> <xsd:complexType name="multiplyResponseType"> <xsd:all> <xsd:element name="return" type="xsd:string" form="unqualified"/> </xsd:all> </xsd:complexType> <xsd:element name="add" type="tns:addRequestType"/> <xsd:element name="addResponse" type="tns:addResponseType"/> <xsd:element name="multiply" type="tns:multiplyRequestType"/> <xsd:element name="multiplyResponse" type="tns:multiplyResponseType"/> </xsd:schema> </types> <message name="addRequest"> <part name="parameters" element="tns:add" /></message> <message name="addResponse"> <part name="parameters" element="tns:addResponse" /></message> <message name="multiplyRequest"> <part name="parameters" element="tns:multiply" /></message> <message name="multiplyResponse"> <part name="parameters" element="tns:multiplyResponse" /></message> <portType name="MathOperationServicePortType"> <operation name="add"> <documentation>Add Parameters</documentation> <input message="tns:addRequest"/> <output message="tns:addResponse"/> </operation> <operation name="multiply"> <documentation>Multiply Parameters</documentation> <input message="tns:multiplyRequest"/> <output message="tns:multiplyResponse"/> </operation> </portType> <binding name="MathOperationServiceBinding" type="tns:MathOperationServicePortType"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="add"> <soap:operation soapAction="http://localhost/MathOperationService#add" style="document"/> <input><soap:body use="literal" namespace="http://localhost/MathOperationService"/></input> <output><soap:body use="literal" namespace="http://localhost/MathOperationService"/></output> </operation> <operation name="multiply"> <soap:operation soapAction="http://localhost/MathOperationService#multiply" style="document"/> <input><soap:body use="literal" namespace="http://localhost/MathOperationService"/></input> <output><soap:body use="literal" namespace="http://localhost/MathOperationService"/></output> </operation> </binding> <service name="MathOperationService"> <port name="MathOperationServicePort" binding="tns:MathOperationServiceBinding"> <soap:address location="http://localhost/soap-server.php"/> </port> </service> </definitions> |
The web service is supporting two simple methods add and multiply that are exposed to outer world.
Now the next task is to consume these web services. For that I have simple file, thats going to create soap client using NuSOAP and call the service. The code sample is attached here. Its simple and staraight forward. I have parameterized it for ease of testing. Test url will look like http://localhost/soap-client.php?method=multiply¶m1=56¶m2=67
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?php require('lib/nusoap.php'); // Try to get method and parameters from query string $method = isset($_GET["method"]) ? $_GET["method"] : "add"; $param1 = isset($_GET["param1"]) ? $_GET["param1"] : "4"; $param2 = isset($_GET["param2"]) ? $_GET["param2"] : "4"; // Create the client instance $client = new nusoap_client('http://localhost/soap-server.php?wsdl', true); $err = $client->getError(); if ($err) { echo '<h2>Constructor error</h2><pre>' . $err . '</pre>'; } $param = array("param1" => $param1 ,"param2" => $param2); $result = $client->call($method, array('parameters' => $param)); // Check for a fault if ($client->fault) { echo '<h2>Fault</h2><pre>'; print_r($result); echo '</pre>'; } else { // Check for errors $err = $client->getError(); if ($err) { // Display the error echo '<h2>Error</h2><pre>' . $err . '</pre>'; } else { // Display the result echo '<h2>Result</h2><pre>'; print_r($result["return"]); echo '</pre>'; } } echo '<h2>Request</h2><pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>'; echo '<h2>Response</h2><pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>'; echo '<h2>Debug</h2><pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>'; ?> |
If you go through this code you will see how easy it to call a web service in PHP.
Here is the web service test through SaopUI, a third party web service testing tool.
That’s it for this article.
Good one. This helped me create first php based soap api Thank you.
One feedback though…
$server->service($HTTP_RAW_POST_DATA); had to be replaced with $server->service(file_get_contents(“php://input”)); because of error
Thank you very much, this was a great help to me