Showing posts with label Web Services. Show all posts
Showing posts with label Web Services. Show all posts

Thursday, June 26, 2014

PHP Soap Client Request with SSL Certificate


When required to consume PHP SOAP web services via HTTPS with certificate authentication, we pass our certificate file to the parameter, “local_cert” of the soap client class. In case our certificate and private key are in separate files, we must combine them into one file for this to work.  You can google around on how to create a ‘.pem’ file from a certificate and private key.

The code below is an excerpt from the PHP SOAP Client Example. The point to note here is that we have added the local_cert parameter which contains the value of our certificate file. Most importantly, we have also changed the protocol from http to https for both the wsdl and the soap server location parameters. 

Monday, May 26, 2014

How To Add Header Message To PHP SOAP Client Request

Some web services require header message to be passed along with the body of the soap request. This is often needed for various reasons such as authentication, payment etc. In order to consume such web services successfully, we must define and pass the header element along with the body of the soap request message.

In this tutorial, we will demonstrates how to define and add a SOAP header message to PHP SOAP Client request. You can take a look at the web services we are trying to consume here. A sample request for the method “boom” from the web service is shown below.
From the sample soap request, we noticed that the soap header element, “requestHeader” has two inputs - username and password. We defined these inputs with an associative array. Eg
Next, we used the PHP SoapHeader class to create the soap header by passing it the namespace of the web service (e.g “http://wsdl.example.org/”), the name of the soap header we trying to create (e.g “requestHeader”) and the the soap header inputs defined above (e.g $headerbody).
Finally, we called the __setSoapHeaders($header) method on the soap client object to set the soap header.

See the complete codes below. 


A sample request and response obtained from the above script.

Request:


Response:


Friday, April 4, 2014

How to process Soap header in php SoapServer

To process soap header request in our PHP SOAP Server Example, we will add to our PHP server class, a function with the same name as the header element we are trying to process. Please ensure that you are using the latest version of PHP to avoid any weird behaviour.


See How To Add Header Message To WSDL, on how we created the header element. For this example, our header element is named, requestHeader. So we will create a function  in our server class, with the same name, requestHeader.
The requestHeader element is a complexType with two parameters, which we can process in php as an associative array with two keys.



Our requestHeader function will be called if soap request contains "requestHeader" in SOAP header. For demonstration, we simply stored the requestHeader parameters, username and password, in a private variable, headerInfo, and returned this info back to the client in the boom() function. See the complete server codes below;


The updated client version, on how to add SOAP Header request to PHP SOAP Client, will be available soon.

How To Add Soap Header Message To WSDL

Let's modify the wsdl generated here to include header message. To do this, we define an element, named requestHeader. Eg.
We create a wsdl complex data type for this element, with two parameters, username and password as shown below;


We add the new element and the complex type definition to the wsdl <type> tag, within the <schema> tag. Eg

We also defined the data or interface for our requestHeader via the <message>
   tag. Eg;






We add this data definition to the message part of the wsdl file; eg

Next, we define <soap:header> tag for our requestHeader type eg.


Finally, we add this tag as part of the input for the boom() and getDate() operations immediately after within the <binding> tag. Eg





This is all we need to add header message to our WSDL. The complete wsdl file is shown below.

You can check out the updated server example that handles the soap header request.



Did you find this tutorial useful? Kindly share your comments.


COMPLETE WSDL FILE :

Friday, March 21, 2014

A Complete PHP SOAP Client Example

This is a complete PHP SOAP Client example where we consumed the web services we developed here. You can also take a look at how we generated the WSDL file for the web service.



The code below started by disabling WSDL cache and defined the wsdl location. We also specify the soap server location. This parameter is optional if you define the soap server location in the wsdl file. We then, instantiate the PHP SOAP Client object by passing it the relevant parameters. We call the boom() method from the soap server class with two parameters; first->"PHP SOAP", and last->"Tutorial". The WSDL Tutorial, explained how we created these parameters. We accessed the value of the returned complex type, boomResponse, by calling the variable, "return". Note that "return" is a parameter of type, boomResponse. Check the server example for more information. Optionally, you can call __getLastRequest() and __getLastResponse() on the soap client object to see the last SOAP request and response respectively.











Your comments and suggestions are welcome

A Complete PHP SOAP Server Example




In this tutorial, we will look at how to create a complete PHP SOAP server in WSDL mode. You can take a look at How we generated the WSDL file  for this tutorial.




I assumed that you have already enabled SOAP in your PHP configuration.




From the code below, we first create a simple php class, exampleClass with two functions; boom() and getDate(). We also create corresponding classes, boomResponse and getDateResponse for their return type. From the WSDL file, you will notice that the response type for both boom and getDate is defined as complexType. A complexType is represented as Struct in SOAP, which we can represent as a class in php. Also, note that the names of the response classes must match exactly what you have in your WSDL file.




After creating the appropriate classes for our soap server, we instantiate the PHP SoapServer object in WSDL mode by passing the WSDL file as a parameter. We call the setClass() method on the object to set the class for this soap server and finally call the handle() method to handle soap request.





Kindly leave a comment if you find this tutorial useful. 

Monday, March 10, 2014

How To Generate WSDL for PHP SOAP Server


This is a simple tutorial on how to generate WSDL file for PHP SoapServer class.



For this tutorial to work, you need to enable SOAP in your PHP configuration. If not, you will get this error message if you try to run this tutorial without soap enabled.



Fatal error: Class 'SoapServer' not found in...



Just google around on how to enable soap in php.


INTRODUCTION



PHP 5 SOAP Server class support both WSDL and Non WSDL mode. In WSDL mode, you typically pass a WSDL file as a parameter to the SoapServer class. eg, 


Assuming our server class is in http://localhost/example/server-test.php, we can call http://localhost/example/server-test.php?wsdl to access the WSDL we passed as a parameter to the PHP SoapServer.

In non WSDL mode, we pass NULL to the PHP Server class and set the uri option to the target namespace for the server. eg


Calling http://localhost/example/server-test.php?wsdl, this time around, will result in the error below, because the SoapServer Class in PHP 5.3.5 does not support automatic WSDL generation;

This means that if we need the WSDL from our PHP SOAP Server (as in the case of some clients that need to create a reference to our PHP class/functions), we have to find a way to create it and pass it a parameter to the PHP SOAP Server.

CREATING THE WSDL FILE FOR PHP SOAP SERVER

To setup our PHP Server class to generate WSDL automatically, we can either use the Zend Framework or NuSOAP. In this tutorial, we will focus on how to create the WSDL file manually. The only disadvantage of using this method is that you have to modify the WSDL file whenever you add a new function or change a method signature in your PHP code. So, it is better to write your PHP SOAP Server functions before creating the WSDL.
We create a class, ExampleClass, with two functions, boom and getDate. The boom function takes two parameters (an associative array with two keys), first and last. It retrieves the values and returns the concatenated string. The getDate function simply returns the current server date.

CREATE THE WSDL FILE

Creating a WSDL file from scratch can be very complicated and error prone. To ease this process, we will use an IDE such as Netbeans to create the WSDL file for our PHP functions. We are not going to write any code in Netbeans, but simply use the IDE to define our PHP functions so that Netbeans can generate the WSDL file for us.

1.    1.    Open Netbeans and go to File -> New Project -> Java Web -> Web Application






     2. Click on Next and enter the project name





     3.       Click next on the remaining dialog boxes .





4.       Right-click the ExampleClass node and choose New -> Web Service.




5.       Enter, ExampleClassWS as the Web service name and type org.example.wsdl in Package. Ensure that, Create Web Service from Scratch is selected. Click on finish.




6.       To create our functions in the WSDL, navigate to the design view window shown below





 7.       Click on Add Operation in the visual designer




8.      Enter the function name, boom. Leave the “Return Type” as String. As earlier said, the function, boom, is expecting two parameters, first and last. Click on “Add” on the dialog box to enter these values. Leave the parameter “Type” as String.





9  .     Click OK to exit the dialog box and return to the editor.





10. Repeat step 7 and 8 to create the second function




-> Delete the default hello() method in the source code to remove the hello operation.

11. Go to the Projects tab in the IDE, expand the Web Services node of the ExampleClass project. Right-click the ExampleClassWS node, and choose Generate and Copy WSDL...




12. Specify where you want to store the WSDL file. In our example, the WSDL file will be stored in the root folder of our project, ExampleClass. Click Ok to generate the WSDL file.





13. Go to  netbeans project directory -> ExampleClass, to see the generated WSDL file.



This is all you need to generate the WSDL file for your PHP SOAP Server class.

Copy both ExampleClassWS.wsdl and ExampleClassWS_schema1.xsd, to the location of your PHP SOAP Server e.g http://localhost/example/. Implement your PHP SOAP Server in WSDL mode, passing this WSDL file as a parameter. You can view the complete SOAP Server codes here. And check out the PHP SOAP Client tutorial to see how we consumed this Web Service. 



Find below the generated WSDL file. 
A sample SOAP request and response obtained from calling the boom() method is also shown below;

REQUEST :

RESPONSE :

GENERATED WSDL FILE :
Related Posts Plugin for WordPress, Blogger...